James Dickens James Dickens - 12 days ago 7
C++ Question

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;


When I call
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. 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();
}

Answer

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.