其他分享
首页 > 其他分享> > c – 不继承复制构造函数

c – 不继承复制构造函数

作者:互联网

我有以下代码:

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
}   

派生类应该继承除默认ctor之外的所有基数(它被解释为here).但为什么复制ctor也不是继承的呢?相关问题的论据在这里是不可接受的.

代码用g 4.8.1编译.

解决方法:

因为标准是这样说的. [class.inhctor] / p3,强调我的:

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.

标签:c,c11,inheritance,copy-constructor
来源: https://codeday.me/bug/20190918/1811174.html