继承构造函数的 C++ 可见性

标签 c++ class inheritance constructor using-declaration

<分区>

当我像这样使用 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.

关于继承构造函数的 C++ 可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57695057/

相关文章:

c++ - autoconf 和 automake 的通配符

java - Eclipse 是否有 "vim: set ft=cpp:"或 "-*- c++ -*-"的等价物?

c# - 使用另一个类的类方法

java - 使用装饰器模式时如何保留接口(interface)?

c++ - cocos2dx 3.3如何截取场景?

c++ - (VS2010 C++) 每次运行程序都要执行一条命令?

angular - 在 typescript 中定义抽象类的类型

python - 我如何在 python 中以相反的方式实现内置对象和创建的类之间的操作?

javascript - 这两种原型(prototype)继承的实现有什么区别?

c# - 如何使用基类对象调用派生函数