C2248 - 从2到2继承

时间:2018-06-13 02:12:08

标签: c++ inheritance protected

我有3个班级,1个由另一个班级继承。 A-> B-&℃。 A有一个受保护的成员函数,我试图用C语言设置。

我得到了C2248 - 错误C2248'A :: status':无法访问在类'A'关联

中声明的无法访问的成员

我不允许访问C类中的变量吗?

 class A {
 public:
     A();
     ~A();
 protected:
     char status[4];
 };

 class B: class A {
 public:
     B();
     ~B();
 };

 class C: class B {
 public:
     C(char newStatus[4]);
 };
 C::C(char newStatus[4])
 {
     this.status = newStatus;
 }

1 个答案:

答案 0 :(得分:0)

默认继承策略是私有的。这意味着status将成为B中的私人成员,并且在C中无法访问,请参阅Difference between private, public, and protected inheritance 以获取更多信息。  因此,您想要公开继承。此外,数组不支持赋值,而是使用std::array

#include <array>

class A {
public:
    A();
    ~A();

protected:
    std::array<char,4> status;
};

class B : public A {
public:
    B();
    ~B();
};

class C : public B {
public:
    C(std::array<char,4> const &newStatus);
};
C::C(std::array<char,4> const &newStatus) {
    this->status = newStatus;
}