C++涉及私有继承的编译器错误
有人可以向我解释以下编译器错误吗:
struct B
{
};
template <typename T>
struct A : private T
{
};
struct C : public A<B>
{
C(A<B>); // ERROR HERE
};
指示行的错误是:
test.cpp:2:1: error: 'struct B B::B' is inaccessible
test.cpp:12:7: error: within this context
究竟是什么无法访问,为什么?
Could someone please explain the following compiler error to me:
struct B
{
};
template <typename T>
struct A : private T
{
};
struct C : public A<B>
{
C(A<B>); // ERROR HERE
};
The error at the indicated line is:
test.cpp:2:1: error: 'struct B B::B' is inaccessible
test.cpp:12:7: error: within this context
What exactly is inaccessible, and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是 struct B 的名称屏蔽。
一探究竟:
The problem is name shielding of struct B .
Check it out:
当您执行
A
时,您正在使A
private
从B
继承,这意味着 < code>B::B 是private
,因此您无法构造C
。You are making
A
private
ly inherit fromB
when you doA<B>
, and that means thatB::B
isprivate
so you can't construct aC
.尝试
A< ::B>
或A
。在
C
内部,对B
的非限定引用将获取所谓的注入类名,它是通过基类引入的A
。由于A
私有地从B
继承,因此注入类名称也会随之而来并且也是私有的,因此C 无法访问
。另一天,另一种语言怪癖......
Try
A< ::B>
orA<struct B>
.Inside of
C
, unqualified references toB
will pick up the so-called injected-class-name, it is brought in through the base classA
. SinceA
inherits privately fromB
, the injected-class-name follows suit and will also be private, hence be inaccessible toC
.Another day, another language quirk...