[C++] Copy constructor calling timing ① (Use one object to initialize another object | Assign one object to another object)





1. Timing of calling copy constructor




The copy constructor in the C++ language is a special constructor in the C++ class.

Its function is to create a new class instance object as a copy of the existing instance object;

The main function of the copy constructor is to initialize the newly created object so that its content is exactly the same as the original object;


"Copy constructor" is also called "assignment constructor". This type of constructor has 4 calling opportunities;

  • Use a class instance object to initialize another class instance object;
	// 使用一个对象初始化另外一个对象
	// 直接手动 调用拷贝构造函数
	Student s2 = Student(s1);
  • Assign a class instance object to another class instance object;
	// 将一个对象赋值给另外一个对象
	// 自动调用拷贝构造函数
	Student s2 = s1;
  • The instance object of the class is passed to the function by value, not by pointer or reference;
  • The function directly returns the instance object value of the class, rather than returning a pointer or reference;




2. Use one object to initialize another object




1. Description of copy constructor calling situation


Using one class instance object to initialize another class instance object will automatically call the copy constructor;

	// 使用一个对象初始化另外一个对象
	// 直接手动 调用拷贝构造函数
	Student s2 = Student(s1);

In the following code, the Student(const Student& s) copy constructor is called directly and manually, and an existing Student class instance object is passed to the copy constructor;


2. Code example - using one object to initialize another object


Code example:

#include "iostream"
using namespace std;

class Student
{
    
    
public:
	Student(int age, int height)
	{
    
    
		m_age = age;
		m_height = height;
		cout << "调用带参数构造函数" << endl;
	}

	Student(const Student& s)
	{
    
    
		m_age = s.m_age;
		m_height = s.m_height;
		cout << "调用拷贝构造函数" << endl;
	}

public:
	int m_age;		// 年龄
	int m_height;	// 身高
};

int main()
{
    
    
	// 调用带参数构造函数
	Student s1(18, 170);

	// 使用一个对象初始化另外一个对象
	// 直接手动 调用拷贝构造函数
	Student s2 = Student(s1);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
}

Execution result: During execution, the parameterized constructor is first called to create the first instance object, and then the copy constructor is manually called to pass the first instance object as a parameter to the function. This is a manual call to the copy constructor;

调用带参数构造函数
调用拷贝构造函数
Press any key to continue . . .

Insert image description here





3. Assign one object to another object




1. Description of copy constructor calling situation


Assign a class instance object to another class instance object;

	// 将一个对象赋值给另外一个对象
	// 自动调用拷贝构造函数
	Student s2 = s1;

In the following code, the Student s1(18, 170) object is assigned to the Student s2 object;

You can use the equal sign method of the constructor to call comprehension, which is equivalent to calling the Student(const Student& s) constructor;


Use the equal sign = to perform assignment operations, which are two completely different concepts from initialization;


2. Code example - assign one object to another object


Code example:

#include "iostream"
using namespace std;

class Student
{
    
    
public:
	Student(int age, int height)
	{
    
    
		m_age = age;
		m_height = height;
		cout << "调用带参数构造函数" << endl;
	}

	Student(const Student& s)
	{
    
    
		m_age = s.m_age;
		m_height = s.m_height;
		cout << "调用拷贝构造函数" << endl;
	}

public:
	int m_age;		// 年龄
	int m_height;	// 身高
};

int main()
{
    
    
	// 调用带参数构造函数
	Student s1(18, 170);

	// 将一个对象赋值给另外一个对象
	// 自动调用拷贝构造函数
	Student s2 = s1;


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
}

Execution result: During execution, the parameterized constructor is first called to create the first instance object, and then the first instance object is directly assigned to the second instance object, during which the copy constructor is automatically called;

调用带参数构造函数
调用拷贝构造函数
Press any key to continue . . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132845553