Yes.
Many programmers don't fully understand what it means to be a "const" member function. Given this class:
1 2 3 4 5 6 7 8 9 10 11
|
class Foo {
int x;
std::string& s;
std::vector<float>* v;
public:
Foo( std::string& s ) : x( 42 ), s( s ), v( new std::vector<float>() ) {}
void f();
void g() const;
};
| |
When you call a const member function, the "this" pointer becomes const. This is well known. But in practice, what that means is that Foo's data members are now effectively this:
1 2 3 4 5
|
class Foo {
int const x ;
std::string& const s;
std::vector<float>* const v;
};
| |
Notice where the "const" is. So, reading the declarations: s is now a constant reference to a std::string, and v is now a constant pointer to a vector. These declarations basically mean that the
reference is constant, not what the reference refers to, and the
pointer is constant, not the thing that the pointer points to.
In the reference case, the const is irrelevant since references can't be rebound anyway. In the pointer case, it means you can't change the pointer value (you can't make v point to a different vector), but you can change the vector that v points to.