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

时间:2015-09-03 15:59:13

标签: c++ oop

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

'A::x' : cannot access protected member declared in class 'A'.

由于继承,不应该能够访问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");
}

1 个答案:

答案 0 :(得分:6)

B可以访问A的成员x,但只能访问其继承的成员x。它无法访问另一个A a.x f实例的成员 public function getRatingAttribute() { return $this::join('reviews', 'accounts.id', '=' , 'reviews.account_id') ->where('accounts.id', $this->attributes['id']) ->select(DB::raw('SUM(reviews.rating) / COUNT(reviews.id)'))->pluck('rating'); }

相关问题