[C++] Copy constructor calling timing ② (Object value as function parameter | Object value as function return value)

Blog summary:

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

  • ① Use one object to initialize another object: Use a class instance object to initialize another class instance object;
	// 使用一个对象初始化另外一个对象
	// 直接手动 调用拷贝构造函数
	Student s2 = Student(s1);
  • ② Assign one object to another object: assign a class instance object to another class instance object;
	// 将一个对象赋值给另外一个对象
	// 自动调用拷贝构造函数
	Student s2 = s1;
  • ③ Object value as function parameter: The instance object of the class is passed to the function in the form of value, not in the form of pointer or reference;
// 定义函数, 接收 Student 对象值作为参数
void fun(Student s)
{
    
    
}
  • ④ Object value as function return value: The function directly returns the instance object value of the class, not a pointer or reference;
// 定义函数, 返回 Student 对象值作为返回值
Student fun()
{
    
    
	Student s1(18, 170);
	return s1;
}




1. Concept of 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;






2. Object value as function parameter




1. Description of copy constructor calling situation


The instance object of the class is passed to the function by value, not by pointer or reference;


In this case, the instance object value of the class is used as a parameter. As opposed to the object value,

  • object pointer
  • object reference

Define the function void fun(Student s). The formal parameter of this function is a Student type object.


// 定义函数, 接收 Student 对象值作为参数
void fun(Student s)
{
    
    
}

If you call this function, you need to copy the actual parameters and pass the copy value of the actual parameters, that is, the object value, to the function parameter. This process requires calling the copy constructor of the Student class;

The entire operation is completed by the C++ compiler and does not require manual intervention by the developer;


2. Code example - object value as function parameter


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;	// 身高
};

// 定义函数, 接收 Student 对象值作为参数
void fun(Student s)
{
    
    
	
}

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

	// 类对象值作为函数参数
	fun(s1);


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

Results of the :

  • First, Student s1(18, 170) calls the parameterized constructor to create a Student class instance object;
  • Then, pass the created instance object to the fun function. Since the object value is passed, a copy of the object needs to be copied. When copying the copy, the copy constructor of the Student class will be automatically called;
调用带参数构造函数
调用拷贝构造函数
Press any key to continue . . .

Insert image description here





3. Object value as function return value




1. Description of copy constructor calling situation


The function directly returns the instance object value of the class, rather than returning a pointer or reference;


The following code defines a function that returns the Student class instance object created inside the function;

// 定义函数, 返回 Student 对象值作为返回值
Student fun()
{
    
    
	Student s1(18, 170);
	return s1;
}

Since the stack memory will be released after the function scope ends, and the Student object in the stack memory will also be destroyed, the return value of the Student type needs to return a copy, and this copy needs to be created by calling the copy constructor;


2. Code example - object value as function return value


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;	// 身高
};

// 定义函数, 返回 Student 对象值作为返回值
Student fun()
{
    
    
	Student s1(18, 170);
	return s1;
}

int main()
{
    
    

	// 类对象值作为函数返回值
	fun();


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

Results of the :

Insert image description here

Guess you like

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