Search code examples
c++inheritanceinner-classesfriend

Friendship and private nested class inheritance


I'd like to inherit from a nested class, which is in private section of outer class. Is it possible?

class A {
  friend class B;
  friend class C;
  private:
    class NiceNestedClass {
    };
};

class C {
  void a() {
    A::NiceNestedClass works;
  }
};

class B : A::NiceNestedClass{
};

Instantiation of NiceNestedClass is not a problem. But g++ do not allow me to inherit from it. Is there any workaround?

g++ -std=c++11 a.c  -o a
a.c:5:11: error: ‘class A::NiceNestedClass’ is private
     class NiceNestedClass {
           ^
a.c:15:14: error: within this context
 class B : A::NiceNestedClass{

g++ 4.8.4, std=c++11


Solution

  • This is a known gcc bug that was reported back in 2013

    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59482

    Your code is correct and should compile with newer versions of gcc (fixed on gcc4.9 and later). On my side (gcc5.3) it works just fine.