Why can't a derived class call protected member function in this code?

后端 未结 4 1890
挽巷
挽巷 2020-11-27 15:14
#include 

class Base
{  
protected:
    void somethingProtected()
    {
        std::cout << \"lala\" << std::endl;
    }
};

class Deri         


        
4条回答
  •  情深已故
    2020-11-27 15:22

    The Derived class can only access the protected base member in Derived objects. It cannot access the member in objects that are not (necessarily) Derived objects. In the cases that fail, you are trying to access the member via a Base &, and since this might refer to an object that is not Derived, the access can't be made.

提交回复
热议问题