无法访问在类'A'中声明的受保护成员

Shu*_*ury 0 c++ oop

这是我在我的一个OOP课程中的示例中找到的一段代码.当我尝试编译它时,我收到以下错误:

'A::x' : cannot access protected member declared in class 'A'.
Run Code Online (Sandbox Code Playgroud)

由于继承,B不应该能够访问A的受保护的int吗?

#include<iostream>
using namespace std;

class A
{
protected: int x;
public: A(int i = -16) { x = i; }
        virtual A f(A a) { return x + a.x; }
        void afisare() { cout << x; }
};

class B : public A
{
public: B(int i = 3) :A(i) {}
        A f(A a) { return x + a.x + 1; }
};

int main()
{
    A *p1 = new B, *p2 = new A, *p3 = new A(p1->f(*p2));
    p3->afisare();
    system("Pause");
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*oft 6

B可以访问A的成员,x但只能访问它继承的成员.它无法访问(in )x的另一个实例的成员.Aa.xf