This code works with clang but g++ says:
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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
Bis allowed to call the protected constructor ofAonly in order to construct its own base subobject.Check this related issue in g++. It was closed as not a bug.