Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个类,只有特定类层次结构中的类才真正需要。我想知道是否可以将类嵌套在最高类的受保护部分并让所有其他类自动继承它?
“继承”是一个错误的词,因为它在 C++ 中有一个非常具体的定义,你不是这个意思,但是你可以这样做。这是合法的:
class A { protected: class Nested { }; }; class B : public A { private: Nested n; };
不在 A 中的代码或从 A 派生的代码无法访问或实例化 A::Nested。