4

When I use using like this why is the constructor inherited publicly?

class Base {
int x;
public:
    Base(int x);
};

class Derived : public Base {
    using Base::Base;
};

I can now do:

Derived d (2);

I thought that using declarations had the visibility of where they are situated. Here, it should be private.

From The C++ Programming Language:

A name brought into a derived class scope by a using-declaration has its access determined by the placement of the using-declaration;

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

1 Answers1

6

According to the C++17 Standard (10.3.3 The using declaration)

19 A synonym created by a using-declaration has the usual accessibility for a member-declaration. A using-declarator that names a constructor does not create a synonym; instead, the additional constructors are accessible if they would be accessible when used to construct an object of the corresponding base class, and the accessibility of the using-declaration is ignored.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335