我有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;
}
答案 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;
}