scope in inheritance
I have a class say, "A" which is having a pure virtual function "foo". The function foo is public in class A.
I create another class B, which inherits from A. It defines "foo" as private.
But if I create instance of A :
A *pB = new B();
pB->foo(); then this calls the private function in B.
I wonder how does this work!
I think you figured out how it works. Calling a function takes the scope of the static type's version. Of course, a virtual function will essentially redirect it to the version of the dynamic type, but at that point, it's already determined that the function is public from the static type. Or something...
The lesson learned is, in order to maintain data-hiding, members you intend to be private (or protected) in derived classes should be declared protected in your base class.
Well, there are uses for having public members in the base that are hidden in the derived class. I forgot which, but I saw a discussion about this point before (probably in contrast to Java, which disallows this), and someone presented a nice use case.