Analysis of the application scenarios of C++ copy constructor

1. Function structure definition

        Before understanding the copy constructor, it is still necessary to review the basic concept of the constructor of the following class. A class constructor is a special member function that is executed when an object of the class is created. The function name of the constructor is the same as the class name, it will not return any type, and the return type is not void, and the general constructor is used to initialize the member variables of the class.

class Object
{
   public:
      Object();  // 构造函数
   private:
      Object obj;
};
//构造函数结构定义
Object::Object(void)
{
    obj = 0;
    cout << "Object is init" << endl;
}

        The copy constructor, also known as the copy constructor, literally creates a new object by copying the object. Therefore, it uses the previously created object in the same class by the compiler to invoke the implementation to complete the initialization of the newly created object. Its function structure is as follows:

Object::Object(const Object& obj)
{
}

        The formal parameter of the copy constructor must be a reference, that is, "&" is added before the parameter. The use of references is because if the actual parameter is passed to the formal parameter by value, the intermediate process needs to go through the process of object copy, and the object copy needs to call the copy constructor. Therefore, in order to prevent infinite construction, an infinite loop recursion is formed, copy The formal parameter of the constructor must be a reference to an object.

        The copy constructor parameter can be a const reference or a non-const reference. However, const is generally used, so that constant objects (objects whose value cannot be changed after initialization) can be used as parameters to initialize other objects, and non-constant objects can also be used as parameters to initialize other objects.


2. Routine description

        If you do not explicitly declare a copy constructor in the class, the compiler will automatically generate a copy constructor for the class. The automatically generated copy constructor has a simple function, that is, copy all the members of the source object for the new object.

#include<iostream >
using namespace std;
class Object
{
public:
   Object(int a, int b);  //构造函数
   void display();
private:
    int m_a;
    int m_b;
};
Object::Object(int a, int b)
{
    m_a = a;
    m_b = b;
}
void Object::display()
{
    cout<<m_a <<","<<m_b<<endl;
}

void main()
{
    Object obj1(1, 9);
    Object obj2(obj1);  //用默认拷贝构造函数初始化obj2 
    obj2.display();  
}

        The result of the operation is:

1,9

        It can be seen that the default function of the copy constructor is to copy the values ​​of the previously created object members to the values ​​of the new object members.

        Generally, you can explicitly declare a copy constructor. If you write a copy constructor, then the default copy constructor does not exist, and you can add custom content to the copy constructor you write.

#include<iostream >
using namespace std;
class Object
{
public:
   Object(int a, int b);  //构造函数
   Object(const Object& obj); //拷贝构造函数
   void display();
private:
    int m_a;
    int m_b;
};
Object::Object(int a, int b)
{
    m_a = a;
    m_b = b;
    cout<<"copy function init"<<endl;
}
Object::Object(const Object& obj)
{
    m_a = obj.m_a;
    m_b = obj.m_b;
}
void Object::display()
{
    cout<<m_a <<","<<m_b<<endl;
}

void main()
{
    Object obj1(1, 9);
    Object obj2(obj1);  //用编写拷贝构造函数初始化obj2 
    obj2.display();  
}

        The result of the operation is:

copy function init
1,9

        It can be seen that the copy constructor I wrote not only copies the values ​​of the previously created object members to the values ​​of the new object members, but also outputs custom print content.


3. Application scenarios

        Generally speaking, the copy function is called in the following basic scenarios:

(1) Initialize the newly created object by using an existing object in the same class.

Object obj2(obj1);   //用拷贝构造函数初始化obj2 
Object obj3 = obj1;  //用拷贝构造函数初始化obj3

(2), copy the object and pass it as a parameter to the function.

#include<iostream>
using namespace std;
class Object 
{
public:
   Object(int a);  //构造函数
   Object(const Object& obj); //拷贝构造函数
public:
    int m_a;
};
Object(int a) 
{ 
    m_a = a; 
};
Object(const Object& obj) 
{
    m_a = obj.m_a;
    cout<<"copy function init"<<endl;
}

void Fun(Object obj)
{ 
}
void main()
{
    Object obj1;
    Fun(obj1);
}

        The result of the operation is:

copy function init

        It can be seen that the formal parameter obj of the Fun() function calls the copy constructor during initialization. That is, the object as a formal parameter is initialized with the copy constructor, and the parameter when calling the copy constructor is the actual parameter obj1 passed when calling the Fun() function.

(3), copy the object and return this object from the function.

#include<iostream>
using namespace std;
class Object 
{
public:
   Object(int a);  //构造函数
   Object(const Object& obj); //拷贝构造函数
public:
    int m_a;
};
Object(int a) 
{ 
    m_a = a; 
};
Object(const Object& obj) 
{
    m_a = obj.m_a;
    cout<<"copy function init"<<endl;
}

Object Fun() 
{
    Object obj1(1);
    return obj1;
}
void main() 
{
    cout <<Fun().m_a<< endl;
}

        The result of the operation is:

copy function init
1

        It can be seen that the return value of the called Fun() function is an object, which is initialized with the copy constructor, that is, when the copy constructor is called, the actual parameter of the copy constructor is the Fun() function return The object returned by the statement.


↓↓↓ For more technical content and book information acquisition, please pay attention to "Mingjie Embedded" for technical exchanges in the group ↓↓↓ 

Guess you like

Origin blog.csdn.net/helloqusheng/article/details/132128218