Inaccessible type due to private inheritance

╄→гoц情女王★ 提交于 2019-12-03 19:51:54

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/8011090/inaccessible-type-due-to-private-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!