问题描述
class A{
public:
A(){};
};
class B : public A{
public:
using A::A;
B(const B&) = default;
B( B&&) = default;
};
B b;
编译器(g ++(5.4.0-6ubuntu1)/c ++ 11)说对B :: B()的调用没有匹配函数",并列出了复制和移动构造函数作为候选对象.如果我将那些默认值注释掉,它将进行编译.是什么原因造成的?明确指定它们为默认值有什么区别?如果那两条线不在那儿,它们将仍然是默认值.
The compiler (g++ (5.4.0-6ubuntu1) / c++11) says "no matching function for call to B::B()" and lists the copy and move constructors as candidates. If I comment those defaulted ones out then it compiles. What causes this? And what difference does it make that they are explicitly defaulted? If those 2 lines weren't there they would be defaulted anyway.
推荐答案
在C ++ 17之前,基类的默认构造函数将不是通过using
继承:
Before C++17, the default constructor of the base class won't be inherited via using
:
在C ++ 17之后,代码可以正常工作.
After C++17 the code works fine.
在此之前,默认构造函数不会从基类继承,也不会是的rel ="nofollow noreferrer">,因为提供了复制/移动构造函数.
Before that, the default constructor won't be inherited from the base class, and won't be generated for class B
because copy/move constructor are provided.
这就是为什么如果您注释掉复制/移出构造函数,它将编译的原因.您可以将定义明确添加为C ++ 17之前的解决方法.例如
That's why if you comment copy/move constructor out it compiles. You can add the definition explicitly as a pre-C++17 workaround. e.g.
class B : public A {
public:
B(const B&) = default;
B( B&&) = default;
B() = default;
};
这篇关于C ++默认构造函数未使用"using"继承.当存在移动和复制构造函数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!