C++ Const Overloading – Public and Private Lookup

c++

The following code does not compile, saying " error C2248: 'A::getMe' : cannot access private member declared in class 'A'". Why? I am trying to call the public interface.

class B
{
};

class A
{
public:
    const B& getMe() const;
private:
    B& getMe(); 
};

int main()
{
A a;
const B& b = a.getMe();
return 0;
}

Best Answer

Part of the problem which wasn't mentioned in other answers is that accessibility and visibility are independent concepts in C++. The B& A::getMe() private member is visible in main even if it isn't accessible. So in your call a.getMe() there are two overloaded members to consider, B& A::getMe() and B const& A::getMe() const. As a is not const it is the private member which is selected. Then you get an error because it isn't accessible. If you hadn't the private non const member function, you would have the const member as the only possibility and it would have be called as a const member can be called on non const object.

Note that if visibility was conditioned to accessibility, you could have other kind of confusing behavior: you refactor a member, putting a call to a private member outside the class. Now, the private member is no more accessible and so the call is to a different member which is public. That silent change of behavior can lead to bugs hard to track.

In conclusion: whatever are the rule of your language, never overload with different accessibility, it leads to confusion.