In C ++, when you write this:
class A { public: A() { ... } };
The compiler really generates this, depending on what your code uses:
class A { public: A() { ... } ~A() { ... } A(const A& other) {...} A& operator=(const A& other) { ... } };
So, now you can see the different semantics of various constructors.
A a1; // default constructor A a2(a1); // copy constructor a2 = a1; // copy assignment operator
Copy constructors basically copy all non-static data. They are generated only if the resulting code is legal and normal: if the compiler sees types inside the class that it does not know how to copy (for the usual assignment rules), then the copy constructor will not be generated. This means that if the type T does not support constructors or if one of the open fields of the class is const or a reference type, then, for example, the generator will not create them, and the code will not be created. Templates expand during build, so if the resulting code cannot be created, it will fail. And sometimes he fails out loud and very mysterious.
If you define a constructor (or destructor) in a class, the generator will not generate a default value. This means that you can override the default constructors. You can make them private (they are publicly accessible by default), you can redefine them so they donβt do anything (useful for preserving memory and preventing side effects), etc.
Andreia Gaita
source share