The copy constructor performs initialization in the following three cases:
1. Use an object in the declaration statement to initialize another object;
2. generate an object copy when passing an object as a parameter value-based call method to another object;
3. Generate a temporary object as the return result of the function.
Next, let's take a look at the time when the copy constructor calls in these three cases, and when to analyze the constructor if there are temporary objects. Assume there is a class Foo, And the constructing will be output in the constructing. ", the copy constructing will be output in the copy constructing. "," destructing. ". Annotations in the output results are used for illustration.
Use one object in the declaration statement to initialize another object
When initializing another object with an object in the declaration statement, there is no problem with the temporary object. The new object directly calls the copy constructor to initialize the object, and there is no call order problem, for example:
Program code: [copy the code to the clipboard]
- Foo obj1;
- Foo obj2 = obj1;
If the class Foo has a copy constructor, the copy constructor will be called during definition. At the end of the object's life cycle, the object's destructor will be called. The output result of the above program is:
Program code: [copy the code to the clipboard]
- Constructing. // constructing obj1
- Copy constructing. // constructing obj2
- Destructing. // destructing obj2
- Destructing. // destructing obj2
Generate an object copy when passing an object as a parameter value-based call method to another object
When an object is called by value as a function parameter, a temporary object is generated at the beginning of the function. If the object has a copy constructor, the temporary object is initialized using the copy constructor, then, call the destructor of the temporary object at the completion of the function, for example:
Program code: [copy the code to the clipboard]
- Void get_object (pobj ){
- Foo OBJ;
- Return;
- }
In the above Code, the initialization of the parameter pobj is executed first, and then the local object variable obj is initialized. The output result of the above Code is as follows:
Program code: [copy the code to the clipboard]
- Copy constructing. // copy constructing pobj
- Constructing. // constructing OBJ
- Destructing. // destructing OBJ
- Destructing. // destructing pobj
Generate a temporary object as the return result of the Function
When a temporary object is generated as the result returned by the function, if a copy constructor is returned, the copy constructor of the returned result is called for initialization and executed when the return statement is executed, the Destructor is called when return is complete, for example:
Program code: [copy the code to the clipboard]
- Foo get_object (){
- Foo OBJ;
- Return OBJ;
- }
- Foo myobj;
- Myobj = get_object ();
Inside the get_object () function, initialize OBJ first. When the return statement is executed, create a temporary object and call the copy constructor to initialize the temporary object using OBJ as the parameter,Then, the structure of the object variables inside the function is first performed, and the structure of the temporary variables is then performed.Because temporary variables must assign values to variables that may receive returned values outside the function. The output result of the above Code is as follows:
Program code: [copy the code to the clipboard]
- Constructing. // constructing myobj
- Constructing. // constructing OBJ
- Copy constructing. // copy constructing temp OBJ
- Destructing. // destructing OBJ
- Destructing. // destructing temp OBJ
- Destructing. // destructing myobj
In this way, after debugging three examples in VC, we can figure out the call time of the copy constructor.