private virtual function in derived class
- by user1706047
class base {
public:
virtual void doSomething() = 0;
};
class derived : public base {
**private:**
virtual void doSomething(){cout<<"Derived fn"<<endl;}
};
now if i do the following:
base *b=new child;
b->doSomething(); //it calls the derived class fn even if that is private.
Question:
1.its able to call the derived class fn even if that is private.How is it possible?
Now if i change the inheritance access specifier from public to protected/private then i get compilation error as "'type cast' : conversion from 'Derived *' to 'base *' exists, but is inaccessible"
Notes: I am aware on the concepts of the inheritance access specifiers.So in second case as its derived private/protected, its inaccessible. But here it confuses me for the first question. Any input will be highly appreciated