如果我声明一个派生类,它还包含一个基类型的附加成员,我会收到一个"constructor is protected"错误.
TEST.CPP:
class Base { protected: Base() {} };
class Derived1 : public Base
{
Derived1() {}
};
class Derived2 : public Base
{
Derived2() {}
Base other_base;
};
Run Code Online (Sandbox Code Playgroud)
$ g ++ test.cpp
test.cpp: In constructor ‘Derived2::Derived2()’:
test.cpp:3:25: error: ‘Base::Base()’ is protected
class Base { protected: Base() {} };
^
test.cpp:12:14: error: within this context
Derived2() {}
Run Code Online (Sandbox Code Playgroud)
如果我宣布Derived2为Base的朋友,那么错误就会消失.谁能解释一下这里发生了什么?
TIA.