bitmask
bitmask

Reputation: 34674

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.

Upvotes: 15

Views: 925

Answers (2)

avakar
avakar

Reputation: 32685

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) {}

Upvotes: 14

rerun
rerun

Reputation: 25505

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.

Upvotes: 8

Related Questions