为什么在随后的私人派生类中简单提及基指针/引用被禁止? [英] Why a simple mention of base pointer/reference in subsequent privately derived classes is forbidden?
问题描述
struct B {};
struct D : private B {
B* fun () { return new D; } // ok
}
struct DD : public D {
B* foo () { return 0; } // error: ‘struct B B::B’ is inaccessible !
};
这个错误对我来说似乎不合理。如果我们可以在全局范围中使用简单 B *
,那么为什么不在其私有派生类? g ++ demo 。
This error seems unreasonable to me. If we can use simple B*
in global scope then why not in its privately derived classes? g++ demo.
我们不会尝试转换 DD *
到 B *
,这是语言规则禁止的( ,此,此是相关问题)。
请注意,如果我将 B * foo()
更改为 int foo()
好的。
We are Not trying to convert DD*
to B*
, which is forbidden by the language rules (this, this, this are related questions).
Note that, if I change B* foo()
to int foo()
, things go fine.
推荐答案
显然,编译器认为 B
B
的私人构造函数。
So apparently the compiler thinks B
is referring to the private constructor of B
rather than the type.
符合 B
显然修正了这个错误:
Qualifying B
apparently fixes that error:
class B* foo () { return 0; }
或此:
::B* foo () { return 0; }
我不知道为什么会发生,但也许这会有帮助。
I don't know why that's happening, but maybe this will help.
更新:也许它与11.2.4的标准相关?唯一的问题是我的标准版不够好,不能完全理解。
Update: maybe it's related to 11.2.4 of standard? The only problem is that my standardese isn't good enough to fully understand it.
(对不起图片,复制/粘贴不适用于我)
(sorry for the image, copy/pasting isn't working for me)
这篇关于为什么在随后的私人派生类中简单提及基指针/引用被禁止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!