无法访问的类型由于私有继承 [英] Inaccessible type due to private inheritance
问题描述
g ++
是拒绝我访问类型,只是因为它恰好是一个私人的父亲。这是否有意义?
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) {}
};
编译这会产生:
1:10: error: ‘struct A A::A’ is inaccessible
6:12: error: within this context
我的观点是:我从来不想访问 A
作为祖先。事实上,如果 A
是 B
的私有祖先,则不应该对任何人完全不可见, c $ c> B (即 C
)?
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
)?
使用 protected
继承,但在我的例子中它没有什么意义。
Of course, I could use protected
inheritance but in my case it doesn't really make sense.
推荐答案
这是因为注入的类名隐藏
中的全局
。虽然 A
C A
是可见的,它是不可访问的(因为它作为私有导入),因此错误。您可以通过在全局命名空间中查找 A
来访问
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) {}
这篇关于无法访问的类型由于私有继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!