gpt4 book ai didi

c++ - 复制构造函数不是继承的

转载 作者:行者123 更新时间:2023-12-03 20:44:06 26 4
gpt4 key购买 nike

我有以下代码:

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 之外的所有 base ctors(解释为 here )。但是为什么 copy ctor 也没有被继承呢?相关问题的论点在这里是 Not Acceptable 。

代码使用 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++ - 复制构造函数不是继承的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66280592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com