受保护的成员应该可以从派生类访问。 然后,为什么在下面的代码中出现编译错误?
class A {
protected:
A() {};
};
class B : public A {
public:
void g() {
A a; // <--- compiling error: "Protected function A::A() is not accessible ...". Why?
}
};
int main() {
B b;
b.g();
}
我注意到有一个相关的帖子,但是该类中有一个模板类。我的只是一个“普通”班。
Why the derived class cannot access protected base class members?