派生类的一个重要性质
看到protected成员部分关于protected的一个重要性质的表述看不大明白,派生类只能通过派生类对象访问其基类的protected成员,派生类对其基类类型对象的protected成员没有特殊的访问权限。
求解释啊。。。书上的好扯。
#include<iostream> using namespace std; class Base { public: Base():i(0),j(0){}; protected: int i; private: int j; }; class Derived:public Base { Derived():Base(){}; print(const Base &b, const Derived &d) { int num = i; //num = b.i; //error. cannot access protected member declared in class 'Base' num = d.i; //num = d.j; //error. cannot access private member declared in class 'Base' }; }; int main() { return 0; }