I use "pointer to private implementation" classes often. The setter methods of those classes can technically be const member functions, such as this:
class MyPrivateClass
{
public:
int something = 1;
};
class MyClass
{
public:
// TODO: initialize pointer in constructor
// TODO: delete pointer in destructor
// Note how this setter is const!
void setSomething(int something) const
{
p->something = something;
}
private:
MyPrivateClass* p;
};
int main()
{
return 0;
}
This is possible since the compiler only enforces bitwise const, and not logical const, so the above code should compile just fine.
I think those setter methods should NOT be const member functions to let the callers know the object is actually being modified (logically modified, not bitwise modifed, due to the pointer to implementation).
My questions is:
Is there a good reason to make those setter methods const member functions?
Effective C++ advises (in item 3) to always use const whenever possible, but I don't think this applies to my example.
const
. If you actually did have a const object, you don't want setters to be called on it – Racket