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

标签 c++ c++11 inheritance copy-constructor

我有以下代码:

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/

相关文章:

c++ - BFS 的伪代码(来自算法设计,第 2 版)混淆?

c++ - 如何用百分比标记字符串?

c++ - 如何使用 boost::asio::io_service 在 C++11 线程之间调度作业

c++ - 无法在 C++ 模板的初始化列表中使用 lambda

java - 为什么父类(super class)在这个程序中首先被执行 - 尽管我没有 main 方法?

c++ - 使用 rdbuf 复制文件并从文件中读取不一致?

c++ - 为什么我在删除 char* 时遇到内存异常

c++ - 使用继承信号解决 Q_PROPERTY NOTIFY

java - 使用父类(super class)/子类引用引用新对象 JAVA

c++ - std::numeric_limits<T>::digits 应该代表什么?