Definition of copy constructor in C + +:
The type of one parameter is the constructor of its class type is a copy constructor.
As shown below:
X::X (const x& X);
Y::y (const y& Y, int =0);
Can be a multiple-parameter form, but its second, successor parameter has a default value
Second, the application of the copy constructor function:
When a class object takes on another homogeneous entity as an initial value, the copy constructor is invoked in most cases. These are generally three specific situations:
1. Explicitly using a class object as the initial value of another class object, shaped like x xx=x;
2. When the class object is passed as a parameter to the function.
3. When a function returns a class object.
The latter two situations produce a temporary object.
Third, C + + compiler when the copy constructor is synthesized
Not all class compilers that do not have a copy constructor are able to synthesize copy constructors for them, and the compiler will synthesize copy constructors only when necessary. The so-called necessary time refers to the compiler in the ordinary means can not complete the solution "when a class object with another homogeneous entity as the initial value", will synthesize the copy constructor. In other words, when the conventional means can solve the problem, there is no need to use unconventional means.
If a class does not define a copy constructor, it usually solves "one class object with another homogeneous entity as the initial value" by "Member initialization (Default memberwise initialization)"-- That is, to copy a built-in or derived data member from one object to another, and if the data member is an object, recursively use the "Member initialization (Default memberwise initialization)" approach.
Member initialization (Default memberwise initialization) is implemented in a bit successive copy (bitwise copy semantics) 1. This means that the compiler does not need to synthesize a copy constructor when it is possible to use this conventional approach to solve the "one class object with another homogeneous entity as the initial value". But sometimes conventional weapons are less useful, and we have to sacrifice unconventional weapons--copy constructors. One of the following situations is that a bit successive copy will not be competent or suitable to complete the work of "one class object using another homogeneous entity as the initial value". At this point, if the class does not have a copy constructor defined, the compiler will have to synthesize a copy constructor for the class.
When a class contains a member object, and the latter class declares a copy constructor (either designer-defined or compiler-synthesized).
When a class inherits from a class that declares a copy constructor (again, whether the copy constructor is displayed or synthesized by the compiler).
Declaration of virtual functions in a class
When a derived string chain of a class contains one or more virtual base classes.
For the first two cases, whether it is a base class or an object member, since the latter declares a copy constructor, it indicates that the designer of its class or the compiler wants to do the work of "one class object as an initial value with another homogeneous entity" by its declared copy constructor, and the designer or compiler does so--declares the copy constructor, There are always reasons for them, and often the most direct reason is that they want to do some extra work or a "bit successive copy" is not competent.
For classes with virtual functions, it is sufficient to have a copy of the same type of two objects. However, the problem will occur if the base class is initialized by its inheriting class. At this point, if this work is done by a bit-by-copy, the vptr of the base class will point to the virtual function table of its inheriting class, which will lead to unpredictable consequences-calling an incorrect virtual function entity is unavoidable and light brings the program crashing, A worse problem may be that the error is hidden. Therefore, a class compiler with virtual functions will explicitly point the vptr of the initialized object to the correct virtual function table. Therefore, a class that has a virtual function does not declare a copy constructor, the compilation will synthesize one for it, to do so, and to initialize each data member, and the declaration of a copy constructor will also be inserted into the code that completes the above work.
For instances in which there is a virtual base class in the inheritance string chain, the problem also occurs when the inheriting class provides an initial value to the base class, where successive copies of the bit may break the position of the virtual base class child object in the object.