C++受保护的ctor对派生类中的成员不可用

1 c++ inheritance

如果我声明一个派生类,它还包含一个基类型的附加成员,我会收到一个"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.

Som*_*ude 7

这是因为它other_base实际上不是Derived2类的一部分,它是一个单独的对象,遵循公共/受保护/私有成员的常规规则.