Do you know the characteristics and usage scenarios of anonymous objects?

Table of contents

1. The concept of anonymous objects

2. Anonymous objects for single-parameter and multi-parameter construction scenarios

① Constructor with only one parameter

② Constructor with multiple parameters

3. Use anonymous objects as default values ​​for function parameters

4. When only calling a function in the class

Five, the characteristics of anonymous objects

1. The life cycle of an anonymous object has only one line

2. Anonymous objects are constant

3. When an anonymous object is referenced, the life cycle of the anonymous object will become longer


1. The concept of anonymous objects

anonymous object:

There is no object name, and no object name is required, directly use the class name to create the object, such as:

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int a = 1, int b = 7)
	{
		_a = a;
		_b = b;
	}
	~Date()
	{
		cout << "析构~Date()" << endl;
	}
private:
	int _a;
	int _b;
};
int main()
{
	Date();//匿名对象
	return 0;
}

2. Anonymous objects for single-parameter and multi-parameter construction scenarios

For single-parameter constructors, C++ supports the use of the equal sign (=) to initialize the newly created object

① Constructor with only one parameter

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year )
	{
		_year = year;
	}
	~Date()
	{
		cout << "析构~Date()" << endl;
	}
private:
	int _year;
};
int main()
{   //编译时,实际变为了 Date d = Date(2023)
	Date d = 2023;//这一步发生了隐式类型转换
	return 0;
}

When using the equal sign (=) to create an anonymous object, implicit type conversion occurs. First, an anonymous object (temporary object) is generated through the constructor, and then the value of the object is assigned to the newly created object d through copy construction.

But the compiler thinks this is too slow, so it optimizes this line directly, so that only the constructor is called

② Constructor with multiple parameters

At this time, you can use the default parameters or use the equal sign (=) to initialize the newly created object.

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year,int month=5,int day=10 )
	{
		_year = year;
		_month = month;
		_day = day;
		
	}
	~Date()
	{
		cout << "析构~Date()" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{   //编译时,实际变为了 Date d = Date(2023)
	Date d = 2023;//这一步发生了隐式类型转换
	return 0;
}

Through debugging, it is found that multiple parameters can also use the equal sign (=) to initialize the newly created object

3. Use anonymous objects as default values ​​for function parameters

This usage scenario is relatively common in function templates. When we are not sure about the type of the parameter, we can use the anonymous object as the default value to specify a default value for the parameter, such as:

vector(size_t n, const T& val = T())//vector构造函数

{}
//T()表示匿名对象

Anonymous objects are used in STL to define vector constructors 

4. When only calling a function in the class

When we only want to call a function in a class, we find that it cannot be called without the instantiation of the class. At this time, we must create a class object to call it. Isn’t this very bad, because instantiating an object will apply Space, occupying excess memory space, then we can use anonymous objects to call, such as:

class Solution {
public:
int Sum_Solution(int n) {
//...
return n;
}
};

int main()
{
   // 匿名对象在这样场景下就很好用
  Solution().Sum_Solution(10);
   return 0;
}

At this time, using anonymous objects will not take up extra space, because the declaration cycle of anonymous objects is only one line, and it will be destroyed when the line is changed.

Five, the characteristics of anonymous objects

1. The life cycle of an anonymous object has only one line

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year,int month=5,int day=10 )
	{
		_year = year;
		_month = month;
		_day = day;
		
	}
	~Date()
	{
		cout << _year << '-' << _month << '-' << _day << endl;
		cout << "析构~Date()" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{  
	Date(2023);//匿名对象
	Date d(2023, 5, 9);
	return 0;
}

Through the results, we can find that the life cycle of anonymous objects is only one line , because the order of destruction of objects is first created and then destroyed, and those created later are destroyed first. Obviously, the anonymous object is destroyed first here, and the anonymous object is created first, so it can be explained that the life cycle of the anonymous object is only this line

2. Anonymous objects are constant

When we quote like this, we can find that it is an error

Date& d = Date(2023);//属于权限被放大的错误

When we add const to modify object d:

const Date& d = Date(2023);

 Reason: Anonymous objects are constant as a temporary variable . When we refer to them without const modification, the permission will be enlarged, so an error will be reported. The permission can be reduced and translated but cannot be enlarged.

3. When an anonymous object is referenced, the life cycle of the anonymous object will become longer

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year,int month=5,int day=10 )
	{
		_year = year;
		_month = month;
		_day = day;
		
	}
	~Date()
	{
		cout << _year << '-' << _month << '-' << _day << endl;
		cout << "析构~Date()" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{  
	const Date& d = Date(2023);//引用匿名对象
	Date d2(2023, 5, 9);
	return 0;
}

result:

 From the results, it can be found that the object d2 created first is destroyed first, and the object d2 created first is destroyed first (a reference to an anonymous object).

Seeing this, everyone must have doubts. Isn’t the life cycle of an anonymous object just one line? Why can it be referenced and destroyed later?

Reason: The life cycle of the anonymous object and the object that references it is bound together. At this time, the life cycle of the anonymous object becomes longer. How long is the life cycle of the object that references it? The life cycle of the anonymous object at this time is How long is it. Just like a variable refers to a temporary variable, the lifetime of the temporary variable is also lengthened.

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year,int month=5,int day=10 )
	{
		_year = year;
		_month = month;
		_day = day;
		
	}
	~Date()
	{
		cout << _year << '-' << _month << '-' << _day << endl;
		cout << "析构~Date()" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
const Date& d3 = Date(2023,5,8);//全局对象引用
int main()
{  
	static const Date& d = Date(2023);//局部对象引用
	Date d2(2023, 5, 9);
	return 0;
}

result:

 It can be found that the life cycle of the anonymous object is indeed bound to the life cycle of the object referencing the anonymous object

That’s all for sharing the knowledge of anonymous objects. If there are any mistakes, please feel free to point them out. Thank you, 886! ! !

Guess you like

Origin blog.csdn.net/m0_72532428/article/details/130589385