Changing Function Access Mode in Derived Class

This is legal C++, §11.6/1 says:

Access is checked at the call point
using the type of the expression used
to denote the object for which the
member function is called (B* in the
example above). The access of the
member function in the class in which
it was defined (D in the example
above) is in general not known.

As you noted, Child::Foo() is thus still accessible via the base class, which is in most cases undesired:

 Child* c = new Child;
 Base* b = c;
 c->Foo(); // doesn't work, Child::Foo() is private
 b->Foo(); // works, calls Child::Foo()

Basically, the declaration you refer to in the expression dictates the access mode – but virtual functions undermine that as another function then the named one may actually be invoked.

Leave a Comment