派生类中的静态方法可以调用c++中的受保护构造函数吗?



此代码与clang一起工作,但g++说:

error: ' A::A() ' is protected

class A
{
protected:
    A() {}
};
class B : public A
{
    static A f() { return A(); } // GCC claims this is an error
};

哪个编译器是正确的?

g++是正确的

c++标准& section;11.5/1说"<…>访问必须通过指向派生的类本身的指针、引用或对象<…>"。在构造函数的情况下,这意味着B只能为了构造自己的基子对象而调用A的受保护构造函数。

在g++中检查此相关问题。

最新更新