class Base{
public:
void callF(){ F(); }
private:
void F(){}
};
class Derived: public Base{
public:
void F(){}
};
int main(){
Derived d;
d.callF();
}
Surprisingly for me,the Base F() is called. I don't understand why. F() was declared and defined in Base class as private,so the Derived object doesn't even know about the existence of such a function in Base. Derived class has its own F(), yet that function is ignored. The question is "Why is the Base class F() called? ".
It goes like this.
Base::callF
function is called. It is public so there's no problem in calling it forom main
.Base::callF
function wants to call a function named F
. The only F
visible to Base::callF
is Base::F
. It is private, but callF
is a member of Base
so it gets to see and use all other members, including the private ones.Derived::F
function has nothing to do with any of this. It's just another function, unrelated to Base::F
, which happens to have a similar name.