I\'m preparing for my CPP exam and one of the question is: Can you delete default class constructor and if so, what would be the reason to do so? OK, so obviously you can do it:
There are a few reasons to delete the default constructor.
= delete
is a style thing, as @HolyBlackCat said, but it does clarify your intent by telling the client code to only call the constructor with the parameters.If the second statement was unclear, consider the following example:
class A
{
public:
//A() = delete;
A(int, int) {};
};
If you tried to call the default constructor right now, you would get an error that would look something like (GCC 7.2):
error: no matching function for call to 'A::A()'
However, if you uncommented the line with the = delete
, then you would get the following:
error: use of deleted function 'A::A()'
This makes it explicitly clear that one is trying to use a deleted constructor in comparison to the other error, which is somewhat unclear.