Possible duplicate:
C ++: overriding public \ private inheritance
class base { public: virtual void doSomething() = 0; }; class derived : public base { private: // <-- Note this is private virtual void doSomething() { cout << "Derived fn" << endl; } };
Now, if I do the following:
base *b = new child; b->doSomething();
Question:
- It is able to call a derived function of a class, even if it is private. How is this possible?
Now, if I change the inheritance access specifier from public to protected / private, I get a compilation error:
'type cast' : conversion from 'Derived *' to 'base *' exists, but is inaccessible
Note. I know the concepts of inheritance access specifiers. So in the second case, since it got private / protected, it is not available. But I wonder about the answer to the first question. Any input would be appreciated.
source share