private virtual function in derived class

Posted by user1706047 on Stack Overflow See other posts from Stack Overflow or by user1706047
Published on 2012-11-29T04:45:45Z Indexed on 2012/11/29 5:04 UTC
Read the original article Hit count: 99

Filed under:
 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

© Stack Overflow or respective owner

Related posts about c++