当我像这样使用 using
时,为什么构造函数是公开继承的?
class Base {
int x;
public:
Base(int x);
};
class Derived : public Base {
using Base::Base;
};
我现在可以:
Derived d (2);
我认为 using
声明具有它们所在位置的可见性。在这里,它应该是私有(private)的。
来自 C++ 编程语言:
A name brought into a derived
class scope by a using-declaration has its access determined by the placement of the using-declaration;
根据C++17标准(10.3.3 using声明)
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.