Can you protect nested classes in C ++?

I have a class that only classes in a particular class hierarchy really need. I wanted to know if it is possible to nest a class in a section with protection of the highest class, and all other classes automatically inherit it?

+5
source share
1 answer

“Inherit” is the wrong word to use, because it has a very specific definition in C ++ that you don’t mean, but yes, you can do it. It is legal:

 class A {
   protected:
   class Nested { };
 };

 class B : public A {
   private:
   Nested n;
 };

And code that is not in or something that comes from A cannot access or instantiate A :: Nested.

+8
source

All Articles