[C++] Anonymous objects ① (Introduction of anonymous objects | Introduction to anonymous objects | Concept of anonymous objects | Anonymous object scope - object creation and destruction)





1. Introduction of anonymous objects



Introduction of anonymous objects: In the previous blog [C++] Copy constructor calling timing ② (Object value as function parameter | Object value as function return value) , it was mentioned that if the class object is used as the return value of the function, what is returned is in the function A copy of the class instance object created in, which is essentially an anonymous object;


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

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




2. Introduction to anonymous objects




1. Concept of anonymous objects


In C++ language,

"Anonymous object" refers to an instance object of a class without an explicitly specified name;

Anonymous objects are often used for temporary calculations or passed as parameters/return values ​​of functions;


Anonymous object creation syntax:

类名(构造函数参数)

The following code is to create an anonymous object of the Student class;

Student(12, 170)

2. Anonymous object scope - object creation and destruction


The scope of the anonymous object is limited to the code that creates the anonymous object. After this code is executed, the anonymous object will be automatically destroyed, and the next line of code cannot access the anonymous object created by the previous line of code;

In the following code, an anonymous object of type Student is created and the printfInfo member method of the object is called;

	// 创建匿名对象, 并执行匿名对象函数
	Student(12, 170).printfInfo();

"Anonymous objects" are usually only used within the statement that creates it, and will be destroyed at the end of the statement;

"Anonymous objects" are not suitable for situations where persistence across multiple statements is required;


3. Code example - Create and use anonymous objects


Core code for creating an anonymous object: After creating an anonymous object of type Student, call the function of the anonymous object; after this code is executed, the anonymous object cannot be accessed because the scope of the anonymous object is limited to the expression in which it is located. ;

	// 创建匿名对象, 并执行匿名对象函数
	Student(12, 170).printfInfo();

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(12, 170).printfInfo();


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

Results of the :

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

Insert image description here

Guess you like

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