The application of copy constructor in C++

  • 2020-04-02 02:26:51
  • OfStack

I. definition of copy constructor in C++ :

There is a type of argument whose class type constructor is for the copy constructor.
As follows:


X::X( const X& x); 
Y::Y( const Y& y, int =0 ); 
//It can be multi-argument, but the second and subsequent arguments have a default value

Ii. Application of copy constructor

When a class object has another entity of the same class as its initial value, the copy constructor is most often called. In general, there are three specific cases:

1. Explicitly take one class object as the initial value of another, in the form of X xx= X;
2. When a class object is given to a function as an argument.
3. When a function returns a class object.

The latter two situations produce a temporary object.

When the compiler composes the copy constructor in C++

Not all class compilers that do not have a copy constructor defined will synthesize a copy constructor for it; the compiler will only synthesize a copy constructor for it if necessary. The moment of necessity is when the compiler fails in the ordinary way to resolve that a copy constructor is synthesized when a class object has another entity of the same class as its initial value. That is, there is no need to use unconventional means when conventional ones can solve the problem.

If a class does not define the copy constructor, usually in accordance with the "member one by one, the Initialization (Default Memberwise Initialization)" technique to solve "a class object to another such entity as initial value" - meaning that the data members of built-in or derived to copy from one object to another object, if the data member is an object that is recursive use "member one by one, the Initialization (Default Memberwise Initialization)".

The implementation of member - by - member Initialization is Bitwise copy semantics 1. That is, the compiler does not need to compose a copy constructor when it can use this general approach to solve the problem of "a class object with a similar entity as its initial value." But sometimes conventional weapons don't work so well, so we have to come up with unconventional weapons -- copy the constructor. In one of several cases, bit-by-bit copying will not be adequate or appropriate to do the job of "one class object with another entity of the same class as the initial value." At this point, if the class does not define a copy constructor, the compiler will have to synthesize a copy constructor for the class.

When a class contains a member object whose class declaration has a copy constructor (either defined by the designer or synthesized by the compiler).
When a class inherits from a class that declares a copy constructor (again, whether the copy constructor is declared or synthesized by the compiler).

There are virtual functions declared in the class

When a class contains one or more virtual base classes in its derived chain.
For the first two cases, both members of the base class or object, since the latter statement a copy constructor, it indicates that the class designer or the compiler hope with its statement copy constructor to complete "a class object to another such entity as initial value" of the work, and the designer or the compiler to do so - a statement copy letter number structure, always have their reasons, and usually the most direct reason is because they want to do some extra work or a successive "copy" incompetent.

For classes with virtual functions, bit-by-bit copying is fine if two objects are of the same type. But the problem will appear in, if the base class initialized by the derived class, as if in a successive copy to complete the work, then the base class VPTR points to the virtual function table of a derived class, this will lead to unpredictable consequences - call a virtual function entity error is inevitable, light brings the program crashes, worse the problem may be the wrong be hidden. So for classes with virtual functions the compiler will explicitly point the VPTR of the initialized object to the correct virtual function table. So if a class with a virtual function does not declare a copy constructor, the compiler will compose one for it to do the work above, and initialize the data members, and if a copy constructor is declared, the code to do the work above will be inserted.

In the case where there is a virtual base class in the inheritance chain, the problem also arises when the inheritance class provides the initial value to the base class, in which case the bit by bit copy may destroy the position of the virtual base class child objects in the object.


Related articles: