[C++] Constructor initialization list ⑤ (Anonymous object life cycle | The constructor cannot be called in the constructor)


Summary of constructor initialization list:

  • The initialization list can provide initial values ​​for the member variables of the class;
  • The initialization list can call the constructor of the member variable type of the class to initialize the member variables;
  • The initialization list can use the parameters passed in the constructor;
  • When a class is initialized, according to the definition order, the constructor of the member variable is called first, then the external class constructor is called, and the destructor is just the opposite;
  • The const member variables of the instance object must only be initialized in the initialization list, and all constructors must be initialized;
  • The constructor cannot be called in the constructor;




1. Anonymous object life cycle




1. Description of anonymous object life cycle


The call 类名(构造函数参数)creates an anonymous object;

The life cycle of an anonymous object is limited to the code expression where the anonymous object is located.

Once this line of code is executed, the anonymous object will be destroyed;


The life cycle of an anonymous object is limited to that line of code;


The life cycle of anonymous objects is divided into the following situations:

  • Create an anonymous object separately and do not use variables to receive the anonymous object. The life cycle of the anonymous object is limited to the line of code expression that creates the anonymous object. Once the line of code is executed, the anonymous object will be destroyed;
  • If an anonymous object is used for object initialization, the anonymous object will not be destroyed, but will be converted into a normal object and assigned to a variable;
  • If an anonymous object is used to assign a value to an existing object, the value of the anonymous object will be assigned to the existing object. After the assignment is completed, the anonymous object will still be destroyed after the execution of this line of code is completed;

2. Code example - Anonymous object life cycle


In the following code, in the fun function, the Student(18, 180) code is called to create an anonymous object of type Student. The life cycle of the anonymous object only exists in this line of code. After the execution of this line of code, the anonymous object will be destruct ;

void fun()
{
    
    
	cout << "创建匿名对象之前" << endl;
	// 创建 Student 类型的匿名对象
	// 匿名对象的生命周期 只存在于 这一句话
	Student(18, 180);
	cout << "创建匿名对象之后" << endl;
}

Code example:

#include "iostream"
using namespace std;

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

	~Student()
	{
    
    
		cout << "执行 Student 的析构函数" << endl;
	}

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

void fun()
{
    
    
	cout << "创建匿名对象之前" << endl;
	// 创建 Student 类型的匿名对象
	// 匿名对象的生命周期 只存在于 这一句话
	Student(18, 180);
	cout << "创建匿名对象之后" << endl;
}


int main()
{
    
    
	// 该函数中定义了一个匿名对象
	fun();


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

Results of the :

创建匿名对象之前
执行 Student 的构造函数
执行 Student 的析构函数
创建匿名对象之后
Press any key to continue . . .

Insert image description here





2. Calling the constructor in the constructor




1. The constructor cannot be called in the constructor


Calling a constructor within a constructor is dangerous;

Calling the constructor in the constructor will not initialize the object;

The way the constructor is called will naturally create an anonymous object.

If there is no variable to receive this anonymous object, the anonymous object will be destroyed after the expression in this line is executed;



2. Code example - calling the constructor in the constructor


In the code below,

First define the parameterized constructor,

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

Then, in the parameterless constructor, call the parameterized constructor;

	// 构造函数中 调用 构造函数 是危险行为
	Student()
	{
    
    
		// 构造函数中调用构造函数
		// 此时 创建 的是匿名对象
		// 如果 匿名对象 没有被接收 
		// 就会在 本代码 执行结束后 立刻销毁
		Student(18, 180);
	}

Student(18, 180) calling method will create an anonymous object;

If the anonymous object is not received, it will be destroyed immediately after the execution of this code ends;

The member variables of this instance object have not actually been initialized, and are currently random values;

This is also the reason why the values ​​of the member variables of the printed object are random values;

s.m_age = -858993460 , s.m_height = -858993460

Code example:

#include "iostream"
using namespace std;

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

	// 构造函数中 调用 构造函数 是危险行为
	Student()
	{
    
    
		// 构造函数中调用构造函数
		// 此时 创建 的是匿名对象
		// 如果 匿名对象 没有被接收 
		// 就会 本代码 执行结束后 立刻销毁
		Student(18, 180);
	}

	~Student()
	{
    
    
		cout << "执行 Student 的析构函数" << endl;
	}

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

void fun()
{
    
    
	// 创建 Student 类型的匿名对象
	// 匿名对象的生命周期 只存在于 这一句话
	Student(18, 180);
}


int main()
{
    
    
	// 该函数中定义了一个匿名对象
	//fun();

	Student s;
	cout << "s.m_age = " << s.m_age << " , s.m_height = " << s.m_height << endl;


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

Results of the :

执行 Student 的构造函数
执行 Student 的析构函数
s.m_age = -858993460 , s.m_height = -858993460
Press any key to continue . . .

Insert image description here

Guess you like

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