C++ Primer:Members defined after the protected keyword may be accessed by the members and friends of a derived class. However, these members are only accessible through derived objects. protected members are not accessible to ordinary users of the class
这段话的意思其实就是:
1.可以被派生类的成员和友元访问,但是
2.只能在派生类对象内部访问,派生类的一般使用者无法访问
结合实例来理解
classbase{protected:int m_prot;private:int m_pri;};classderived:public base
{friendintaf(derived &b);public:intas(){
m_prot =5;//OK,派生类成员函数中访问基类的protected成员return0;}};intaf(derived &b){ b.m_prot =9;return0;}//OK,派生类的友元函数中访问intmain(){
derived d;
d.as();//OKaf(d);//OK
d.m_prot;//illegal,派生类的一般使用者无法访问基类的protected成员
base b;
b.m_prot;//illegal,protected成员无法被类的一般使用者访问}
protected 访问说明符C++ Primer:Members defined after the protected keyword may be accessed by the members and friends of a derived class. However, these members are only accessible through derived objects. protected members are not accessible to ordinary users