const 成员函数只能调用 const 成员函数?

bad*_*ash 2 c++ const-method

const 成员函数只调用 const 成员函数吗?

class Transmitter{
  const static string msg;
  mutable int size;
  public: 
    void xmit() const{
    size = compute();
    cout<<msg;
  }
  private:
   int compute() const{return 5;}
};

 string const Transmitter::msg = "beep";

 int main(){
   Transmitter t;
   t.xmit();
   return EXIT_SUCCESS;
 }
Run Code Online (Sandbox Code Playgroud)

如果我不使 compute() 成为一个常量,那么编译器就会抱怨。是不是因为 const 成员函数不允许修改成员,它不允许对非常量的任何调用,因为这意味着 const 成员函数将“间接”修改数据成员?

wil*_*ilx 5

是不是因为 const 成员函数不允许修改成员,它不允许对非常量的任何调用,因为这意味着 const 成员函数将“间接”修改数据成员?

是的。