简体   繁体   English

用delete重载复制构造函数,然后在C ++中调用子类的默认构造函数

[英]Overloading copy constructor with delete and then calling the default constructor of a subclass in C++

I am trying to disable or rather delete the copy constructor of the parent class Card with the line Card(const Card&) = delete; 我试图通过行Card(const Card&) = delete;来禁用或更确切地说删除父类Card的副本构造Card(const Card&) = delete;

When I call Quartz* qu = new Quartz(); 当我调用Quartz* qu = new Quartz(); in the main i get the error that the default constructor is deleted? 在主要我得到默认构造函数被删除的错误? I find this confusing since I did not think I was defining a default constructor in Card but rather an overload of the copy constructor. 我发现这很令人困惑,因为我不认为我在Card中定义了默认构造函数,而是在复制构造函数中重载了。 Any explanations or workarounds for this much appreciated. 任何解释或解决方法对此表示赞赏。

class Card {
public:

    Card(const Card&) = delete;

};

class Quartz : public Card {
public:
    Quartz() = default;


};

int main() {

    Quartz* qu = new Quartz();
}

Default constructor is only implicitly defined if the class has no other constructors. 如果类没有其他构造函数,则仅隐式定义默认构造函数。 Since you defined a copy constructor, you now need to explicitly define default one, too. 由于定义了副本构造函数,因此现在还需要显式定义默认构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM