Wan*_*tIt 5 c++ inheritance protected private-members
为什么基类中的受保护成员在派生类中无法访问?
class ClassA
{
public:
int publicmemberA;
protected:
int protectedmemberA;
private:
int privatememberA;
ClassA();
};
class ClassB : public ClassA
{
};
int main ()
{
ClassB b;
b.protectedmemberA; // this says it is not accesible, violation?
//.....
}
Run Code Online (Sandbox Code Playgroud)
你可以进入protectedmemberA 里面 b.您正试图从外部访问它.它与继承无关.
出现这种情况的原因与以下相同:
class B
{
protected:
int x;
};
//...
B b;
b.x = 0; //also illegal
Run Code Online (Sandbox Code Playgroud)