const member functions can call const member functions only?
Posted
by
Abhi
on Stack Overflow
See other posts from Stack Overflow
or by Abhi
Published on 2011-01-06T11:38:10Z
Indexed on
2011/01/06
11:54 UTC
Read the original article
Hit count: 158
c++
|const-method
Hi all. Do const member functions call only const member functions?
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;
}
If i dont make compute() a const, then the compiler complains. Is it because since a const member function is not allowed to modify members, it wont allow any calls to non-consts since it would mean that the const member function would be 'indirectly' modifying the data members?
© Stack Overflow or respective owner