In depth C++ constructor copy constructor assignment operator destructor call process summary

  • 2020-04-02 01:01:02
  • OfStack

1. When a target object is constructed from the source object of the same class, the copy constructor is called to construct the target object. If the copy constructor is not defined, the default copy function of the class is called to construct the target object.
2. When the return value of a function is an object of a class, if an object is not defined to receive the return object value in the calling function, a temporary object is returned to save the value of the returned object. The temporary object is destroyed at the end of the called function. When there is an accept object in the calling function, the return object is assigned to the receiving object, which calls the destructor at the end of the calling function.
3. When a class has a constructor with a parameter, the object can be initialized with the same type of data as the parameter, and the constructor is called by default.

    #include "stdafx.h"  
    #include <iostream>  
    using namespace std;  
    class B  
    {  
    public:  
        B():data(0)    //Default constructor & NBSP;
        {   
            cout << "Default constructor is called." << endl;  
        }  
        B(int i):data(i) //Constructor with argument & PI;
        {  
            cout << "Constructor is called." << data << endl;  
        }  
        B(B &b)   //Copy constructor & NBSP;
        {  
            data = b.data; cout << "Copy Constructor is called." << data << endl;  
        }  
        B& operator = (const B &b) //Overload of the assignment operator & NBSP;
        {  
            this->data = b.data;  
            cout << "The operator "= " is called." << data << endl;  
            return *this;  
        }  
        ~B() //Destructor & NBSP;
        {  
            cout << "Destructor is called. " << data << endl;  
        }  
    private:  
        int data;  
    };  

    //Function, the argument is a B object, the return value is also a B object & PI;
    B fun(B b)  
    {  
        return b;  
    }  

    //Test function & NBSP;
    int _tmain(int argc, _TCHAR* argv[])  
    {  
        fun(1);  
        cout << endl;  

        B t1 = fun(2);  
        cout << endl;  

        B t2;  
        t2 = fun(3);  

        return 0;  
    }  


 The output result is:   


    Constructor is called.1             //Construct the parameter b  with 1; & have spent & have spent & have spent
    Copy Constructor is called.1      //Construct a temporary object with a b copy, because there is no object to accept the return value of the fun & NBSP; & have spent & have spent & have spent
    Destructor is called. 1            //Parameter b is destructed & NBSP; & have spent & have spent & have spent
    Destructor is called. 1             //Temporary object is destructed & NBSP;
    Constructor is called.2                  //Construct the parameter b  with 2; & have spent & have spent & have spent & have spent & have spent & have spent
    Copy Constructor is called.2           //Construct t1 with a copy of b, and the copy constructor & PI is called; & have spent & have spent & have spent
    Destructor is called. 2                  //Parameter b is destructed & NBSP;
    Default constructor is called.             //Call the default constructor to construct t2& PI; & have spent & have spent & have spent & have spent & have spent & have spent
    Constructor is called.3                       //Construct the parameter b  with 3; & have spent & have spent & have spent & have spent & have spent & have spent
    Copy Constructor is called.3             //Construct a temporary object with a b copy & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent
    Destructor is called. 3                        //Parameter b is destructed & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent
    The operator "= " is called.3              //The call = operator initializes t2, where the assignment operator & PI is called; & have spent & have spent & have spent
    Destructor is called. 3                         //Temporary object is destructed & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent
    Destructor is called. 3                         //T2 is destructed & cake; & have spent & have spent & have spent & have spent & have spent & have spent
    Destructor is called. 2                         //T1 is destructed & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent
     Please press any key to continue . . .  

In addition:
B t1 = fun (2); A and B t2;   T2 = fun (3);   Call the constructor is different, the first call is a copy constructor, the latter call is the "=" operator overload, who can tell me why?

Related articles: