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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With