Possible Duplicate:
Why can i access a derived private member function via a base class pointer to a derived object?
#include <iostream>
using namespace std;
class B {
public:
virtual void fn1(void) {cout << "class B : fn one \n"; }
virtual void fn2(void) {cout << "class B : fn two \n"; }
};
class D: public B {
void fn1(void) {cout << "class D : fn one \n"; }
private:
void fn2(void) {cout << "class D : fn two \n"; }
};
int main(void)
{
B *p = new D;
p->fn1();
p->fn2();
}
Why does p->fn2()
call the derived class function even though fn2
is private in D
?
From wikipedia:
HTH.
The call
p->fn2()
is evaluated at run time depending on the type of objected pointed byp
. At compile time the compile sees thep->fn2()
call as call toB::fn2()
and sinceB::fn2()
is public the compiler doesn't report only error. It is only at runtime that actual function callD::fn2()
is evaluated.This doesn't break the
Encapsulation
principle this is a feature of C++ calledRun-time Polymorphism
orDynamic Polymorphism
When you do
p = new D
,p->__vfptr
is now pointing to the start of the virtual function table ofD
. And since this happens at runtime, therefore, access specifiers don't come into play.Access modifiers, such as
public
,private
andprotected
are only enforced during compilation. When you call the function through a pointer to the base class, the compiler doesn't know that the pointer points to an instance of the derived class. According to the rules the compiler can infer from this expression, this call is valid.It is usually a semantic error to reduce the visibility of a member in a derived class. Modern programming languages such as Java and C# refuse to compile such code, because a member that is visible in the base class is always accessible in the derived class through a base pointer.