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

时间:2023-01-15 20:44:03

This code works with clang but g++ says:

这段代码适用于clang,但g ++说:

error: ‘A::A()’ is protected

错误:'A :: A()'受到保护

class A
{
protected:
    A() {}
};

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

Which compiler is right?

哪个编译器是对的?

1 个解决方案

#1


11  

g++ is right.

g ++是对的。

The C++ Standard §11.5/1 says that "<...> the access must be through a pointer to, reference to, or object of the derived class itself <...>". In case of constructors, this means that B is allowed to call the protected constructor of A only in order to construct its own base subobject.

C ++标准§11.5/ 1表示“<...>访问必须通过指向,引用或派生类本身的对象<...>”。对于构造函数,这意味着只允许B调用A的受保护构造函数,以便构造自己的基础子对象。

Check this related issue in g++. It was closed as not a bug.

用g ++检查这个相关的问题。它被关闭不是一个错误。

#1


11  

g++ is right.

g ++是对的。

The C++ Standard §11.5/1 says that "<...> the access must be through a pointer to, reference to, or object of the derived class itself <...>". In case of constructors, this means that B is allowed to call the protected constructor of A only in order to construct its own base subobject.

C ++标准§11.5/ 1表示“<...>访问必须通过指向,引用或派生类本身的对象<...>”。对于构造函数,这意味着只允许B调用A的受保护构造函数,以便构造自己的基础子对象。

Check this related issue in g++. It was closed as not a bug.

用g ++检查这个相关的问题。它被关闭不是一个错误。