开发者

Can a static method in a derived class call a protected constructor in C++?

开发者 https://www.devze.com 2023-04-01 20:30 出处:网络
This code works with clang but g++ says:开发者_如何学Go error: ‘A::A()’ is protected class A {

This code works with clang but g++ says:

开发者_如何学Go

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

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

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

Which compiler is right?


g++ is right.

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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号