Joh*_*nck 21 c++ static constructor compiler-errors protected
这段代码适用于clang,但g ++说:
错误:'A :: A()'受到保护
class A
{
protected:
A() {}
};
class B : public A
{
static A f() { return A(); } // GCC claims this is an error
};
Run Code Online (Sandbox Code Playgroud)
哪个编译器是对的?
Kir*_*sky 11
g ++是对的.
C++标准§11.5/ 1表示"<...>访问必须通过指向,引用或派生类本身的对象<...>".对于构造函数,这意味着B只允许调用受保护的构造函数A以构造自己的基础子对象.
用g ++ 检查这个相关的问题.它被关闭不是一个错误.