开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜