why can a base pointer point to derived object only under public inheritance?

前端 未结 3 938
野性不改
野性不改 2021-01-05 03:03

I think its because the base class data members and methods wont be accessible , but I\'d like some more clarity on this. Also, is this the reason why polymorphism (using vi

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 03:30

    If I'm right, you are asking about the elements visibility inside a class. As you stated public/protected/private will influence the accessibility of your members/functions members/methods. (see Difference between private, public, and protected inheritance) However polymorphism is not restricted to public inheritance.

    example:

    class B
    {
        protected:
            virtual void do_B() = 0;
    };
    
    class A : protected B
    {
        virtual void do_B() {};
    };
    

提交回复
热议问题