Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 213
Dejwi Avatar asked Jan 27 '16 14:01

Dejwi


2 Answers

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.

like image 160
vsoftco Avatar answered Nov 04 '22 01:11

vsoftco


This might be a bug. Using gcc.godbolt.org and running

#include <iostream>

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

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

class B : A::NiceNestedClass{
};

int main(){

}

Works with every version of clang, ICC and on gcc 4.9.2 or higher. It fails with any gcc of 4.8.x or below.

like image 38
NathanOliver Avatar answered Nov 04 '22 01:11

NathanOliver