As per cppref:

Using-declaration introduces a member of a base class into the derived class definition, such as to expose a protected member of base as public member of derived.

However, the following code can't compile:

class A { protected: A(int, int) { } }; class B : public A { public: using A::A; }; int main() { B(1, 2); // error: calling a protected constructor of class 'A' }

Why can't C++ using-declaration expose a protected member of base as a public member of derived as expected?

Richard's user avatar

Richard

49.6k7 gold badges46 silver badges102 bronze badges

asked Feb 6, 2024 at 13:59

xmllmx's user avatar

2

The behavior of the program can be explained using namespace.udecl#16:

A using-declaration has the usual accessibility for a member-declaration. Base-class constructors considered because of a using-declarator are accessible if they would be accessible when used to construct an object of the base class; the accessibility of the using-declaration is ignored.

Note the emphasis on "the accessibility of the using-declaration that introduced it is ignored". And since the base class constructor A::A(int, int) is protected(meaning it is inaccessible inside main), we get the mentioned error.

answered Feb 6, 2024 at 14:08

Richard's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.