copy constructor

1. What is the copy constructor
First of all, for ordinary types of objects, copying between them is very simple, for example:

    int a = 100; 
    int b = a;

The class object is different from the ordinary object, the internal structure of the class object is generally more complex, and there are various member variables.
Let's look at a simple example of a class object copy.

    #include <iostream> 
    using namespace std; 
     
    class CExample { 
    private: 
         int a; 
    public: 
          //构造函数 
         CExample(int b) 
         { a = b;} 
     
          //一般函数 
         void Show () 
         { 
            cout<<a<<endl; 
          } 
    }; 
     
    int main() 
    { 
         CExample A(100); 
         CExample B = A; //注意这里的对象初始化要调用拷贝构造函数,而非赋值 
          B.Show (); 
         return 0; 
    }

Run the program, the screen outputs 100. It can be seen from the running result of the above code that the system allocates memory for object B and completes the copying process with object A. As far as class objects are concerned, class objects of the same type complete the entire copying process through the copy constructor.
The following example illustrates the working process of the copy constructor.

    #include <iostream> 
    using namespace std; 
     
    class CExample { 
    private: 
        int a; 
    public: 
        //构造函数 
        CExample(int b) 
        { a = b;} 
         
        //拷贝构造函数 
        CExample(const CExample& C) 
        { 
            a = C.a; 
        } 
     
        //一般函数 
        void Show () 
        { 
            cout<<a<<endl; 
        } 
    }; 
     
    int main() 
    { 
        CExample A(100); 
        CExample B = A; // CExample B(A); 也是一样的 
         B.Show (); 
        return 0; 
    }

CExample(const CExample& C) is our custom copy constructor. It can be seen that the copy constructor is a special constructor. The name of the function must be the same as the name of the class, and one of its required parameters is a reference variable of this type.

2. The timing of calling the copy constructor
In C++, the following three objects need to call the copy constructor!
1. Objects pass in function parameters by value

    class CExample  
    { 
    private: 
     int a; 
     
    public: 
     //构造函数 
     CExample(int b) 
     {  
      a = b; 
      cout<<"creat: "<<a<<endl; 
     } 
     
     //拷贝构造 
     CExample(const CExample& C) 
     { 
      a = C.a; 
      cout<<"copy"<<endl; 
     } 
      
     //析构函数 
     ~CExample() 
     { 
      cout<< "delete: "<<a<<endl; 
     } 
     
         void Show () 
     { 
             cout<<a<<endl; 
         } 
    }; 
     
    //全局函数,传入的是对象 
    void g_Fun(CExample C) 
    { 
     cout<<"test"<<endl; 
    } 
     
    int main() 
    { 
     CExample test(1); 
     //传入对象 
     g_Fun(test); 
     
     return 0; 
    }

When calling g_Fun(), the following important steps will occur:
(1). When the test object is passed into the formal parameter, a temporary variable will be generated first, which is called C.
(2). Then call the copy constructor to give the value of test to C. The whole two steps are a bit like: CExample C(test);
(3). After g_Fun() is executed, destruct the C object.

2. Objects are returned from functions by value

    class CExample  
    { 
    private: 
     int a; 
     
    public: 
     //构造函数 
     CExample(int b) 
     {  
      a = b; 
     } 
     
     //拷贝构造 
     CExample(const CExample& C) 
     { 
      a = C.a; 
      cout<<"copy"<<endl; 
     } 
     
         void Show () 
         { 
             cout<<a<<endl; 
         } 
    }; 
     
    //全局函数 
    CExample g_Fun() 
    { 
     CExample temp(0); 
     return temp; 
    } 
     
    int main() 
    { 
     g_Fun(); 
     return 0; 
    }

When the g_Fun() function executes to return, the following important steps will occur:
(1). A temporary variable will be generated first, which is called XXXX.
(2). Then call the copy constructor to give the value of temp to XXXX. The whole two steps are a bit like: CExample XXXX(temp);
(3). At the end of the function execution, the temp local variable is first destructed.
(4). After the execution of g_Fun(), the XXXX object is destructed.

3. The object needs to be initialized by another object;

    CExample A(100); 
    CExample B = A;  
    // CExample B(A);

The last two sentences will call the copy constructor.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324497704&siteId=291194637