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 http://ideone.com/ZFJdQU.
我们并不是要尝试转换DD*
to B*
,这是语言规则所禁止的(this https://stackoverflow.com/questions/9661936/inheritance-a-is-an-inaccessible-base-of-b, this https://stackoverflow.com/questions/4847100/c-inheritance-inaccessible-base, this https://stackoverflow.com/questions/3674876/why-is-this-not-allowed-in-c是相关问题)。
请注意,如果我改变B* foo()
to int foo()
,一切顺利。
所以显然编译器认为B
指的是私有构造函数B
而不是类型。
资格赛B
显然修复了该错误:
class B* foo () { return 0; }
or this:
::B* foo () { return 0; }
我不知道为什么会发生这种情况,但这也许会有所帮助。
更新:也许与标准11.2.4有关?唯一的问题是我的标准语不够好,无法完全理解它。
(抱歉图片,复制/粘贴对我不起作用)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)