Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inaccessible type due to private inheritance

g++ is denying me access to a type, just because it happens to be a private grand-father. Does this make sense?

struct A {};

struct B : private A {};

struct C : B {
  void foo(A const& a) {}
};

Compiling this yields:

1:10: error: ‘struct A A::A’ is inaccessible
6:12: error: within this context

My point is: I never wanted to access A as an ancestor. In fact, if A is a private ancestor of B, shouldn't this be completely invisible to anybody but B (i.e. C)?

Of course, I could use protected inheritance but in my case it doesn't really make sense.

like image 371
bitmask Avatar asked Nov 04 '11 14:11

bitmask


2 Answers

This is due to the injected class name from A hiding the global A inside C. Although A is visible, it is not accessible (since it is imported as private), hence the error. You can access A by looking it up in the global namespace:

void foo(::A const& a) {}
like image 131
avakar Avatar answered Sep 18 '22 08:09

avakar


if you declare it as follows it works

struct A {};

struct B : private A {};

struct C : B {
  void foo(::A const& a) {}
};

The error your seeing is do to name resolution not access. The ::A says look at the global namespace not my inherited nested class types. Also remember that private inheritance is just saying B has an A and IMOHO is a stupid language feature that should be avoided.

like image 24
rerun Avatar answered Sep 18 '22 08:09

rerun