[C++] Anonymous object ② (Initialize "anonymous object" to a variable | Assign "anonymous object" to a variable)


When the C++ compiler finds that an anonymous object is used, it will decide how to handle the anonymous object based on its usage;

  • Anonymous objects are used alone: ​​If the anonymous object is simply used without assigning the anonymous object to other variables, the anonymous object will be destroyed after the expression is executed;
  • Use anonymous objects to initialize variables: If after creating an anonymous object, you also use an anonymous object to initialize variables, the compiler will convert the anonymous object into a normal object and will not destroy the anonymous object. The object will continue until the end of the scope;
  • Use anonymous objects to assign values ​​to variables: If after creating an anonymous object, you also use the anonymous object to assign values ​​to existing variables, the compiler will assign the value of the anonymous object to the existing variable and destroy the anonymous object immediately;




1. Initialize the "anonymous object" to the variable




1. Use anonymous objects for initialization operations


The scope of "anonymous object" is limited to the expression in which it is located. After the expression is executed, the anonymous object is automatically destroyed;

A special case is discussed here. After creating an anonymous object, before the expression is executed, the anonymous object can be used to initialize the ordinary variables, and the anonymous object can be retained. The anonymous object can still be accessed in subsequent code;

The following introduces the principle of the above operation;


2. Convert anonymous objects to ordinary objects


First create an "anonymous object", and then assign the anonymous object to the Student s variable;

// 创建匿名对象, 并将其赋值给变量
Student s = Student(12, 170);

After the C++ compiler recognizes the above operation, it will convert the anonymous object into an instance object with the variable name s;

At this time, even if the statement is executed, the created anonymous object is converted into a normal object and will naturally not be destroyed;


Here, the "anonymous object" is directly converted into an "ordinary object". This is just a simple conversion and does not involve copying;


3. Code example - assign "anonymous object" to a variable


Code example:

#include "iostream"
using namespace std;

class Student
{
    
    
public:

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

	// 打印学生信息
	void printfInfo()
	{
    
    
		cout << "学生信息 : 年龄 = " << m_age  << " , 身高 = " << m_height << 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 s = Student(12, 170);

	// 调用对象方法
	s.printfInfo();


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

Results of the :

调用带参数构造函数
学生信息 : 年龄 = 12 , 身高 = 170
Press any key to continue . . .

Insert image description here





2. Assign "anonymous object" to a variable




1. Use anonymous objects for assignment operations


"Anonymous object" has two uses after creation. One is to initialize variables, which directly converts anonymous objects into ordinary objects and does not involve the destruction of anonymous objects;


The other is to assign the anonymous object to an existing variable. The C++ compiler will handle it as follows:

  • First, read the value of the anonymous object and assign the value to an existing variable.
  • Then, destroy the anonymous object, leaving only the assigned ordinary variables;

2. Code example - using anonymous objects for assignment operations


Code example:

#include "iostream"
using namespace std;

class Student
{
    
    
public:

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

	// 打印学生信息
	void printfInfo()
	{
    
    
		cout << "学生信息 : 年龄 = " << m_age  << " , 身高 = " << m_height << endl;
	}

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

	~Student()
	{
    
    
		cout << "调用析构函数 : m_age = " << m_age << endl;
	}

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

int main()
{
    
    
	// 创建普通对象
	Student s(18, 180);
		
	// 创建匿名对象, 并将其 赋值 给变量
	s = Student(12, 170);

	// 调用对象方法
	s.printfInfo();


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

Results of the :

调用带参数构造函数
调用带参数构造函数
调用析构函数 : m_age = 12
学生信息 : 年龄 = 12 , 身高 = 170
Press any key to continue . . .

Insert image description here

Guess you like

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