The class has a default constructor, a copy constructor, a move constructor, a copy assignment operator, a move assignment operator, and a destructor. If we explicitly specify a constructor with parameters, it will replace the default constructor. And if we explicitly declare the copy constructor, will it replace the default constructor or the copy constructor?

  • And how do you imagine the replacement of the default constructor (without parameters) by the copy constructor (with a parameter)? - Harry
  • What does "replace" mean? Will lead to removal? Will delete? - AnT
  • Under "replace" is understood "will not generate." - free_ze
  • @free_ze: In C ++, the concept of "will not generate" actually falls into two: "will not declare" and "will not determine" and the logic there is not simple. - AnT
  • @AnT And in what case will not determine, but announce? - free_ze

1 answer 1

If we explicitly define any constructor with parameters, the compiler will not generate a default constructor. You can force it to do this explicitly:

class MyType { public: MyType()=default; // сгенери, пожалуйста, умолчательный конструктор MyType(int i):_i(i){} //.... }; 

The copy constructor will not be generated if we provide our version.

To replace the copy constructor, we need to define a constructor whose parameter will be a reference to an object of the same type.

  • default seems to be not a mandatory word. Isn't it enough to just implement a constructor without parameters, filling it with its own functionality. - perfect
  • @perfect is true, but then it will not be a compiler-generated, but a custom default constructor. - free_ze
  • Why then need a method that does nothing? I understand that the compiler agrees with me on this issue. Why such a construction is needed, reveal the secret. - perfect
  • @perfect Why does nothing? It initializes the fields with default values. A custom constructor with an empty body and an initialization list would really look clumsy. And if we had not declared the default constructor in any form and at the same time were any special, we would have completely lost the ability to construct objects by calling the default constructor. - free_ze
  • I understand that to initialize the variable members of the cash register with default values, you need to do this manually in the constructor without parameters. by itself he will not do that. - perfect