问题描述
我有以下代码:
class C {
public:
C(int) {}
C(const C&) {}
C() {}
};
class D : public C {
public:
using C::C;
};
int main() {
C c;
D d_from_c(c); // does not compile, copy ctor is not inherited
D d_from_int(1); // compiles, C(int) is inherited
}
派生类应该继承除默认构造函数之外的所有基构造函数(解释为 here).但是为什么 copy ctor 也没有被继承呢?此处不接受来自相关问题的论点.
Derived class should inherit all ctors of base except the default ctor (it is explained here). But why copy ctor is not inherited as well? Arguments from the related question are not acceptable here.
代码是用g++ 4.8.1编译的.
The code is compiled with g++ 4.8.1.
推荐答案
因为标准是这么说的.[class.inhctor]/p3,强调我的:
Because the standard says so. [class.inhctor]/p3, emphasis mine:
对于候选集合中的每个非模板构造函数构造函数除了没有参数的构造函数或具有单个参数的复制/移动构造函数,构造函数是使用相同的构造函数特性隐式声明,除非有一个用户声明的构造函数具有相同的签名出现 using 声明或构造函数的完整类将是该类的默认构造函数、复制构造函数或移动构造函数.
For each non-template constructor in the candidate set of inherited constructors other than a constructor having no parameters or a copy/move constructor having a single parameter, a constructor is implicitly declared with the same constructor characteristics unless there is a user-declared constructor with the same signature in the complete class where the using-declaration appears or the constructor would be a default, copy, or move constructor for that class.
这篇关于复制构造函数不被继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)