无法访问的类型由于私有继承 [英] Inaccessible type due to private inheritance

查看:148
本文介绍了无法访问的类型由于私有继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆