C++ elementary - initialization list of constructor

Preface: In C++, the initialization list is an extremely important part of the constructor , where each member variable is defined.

1. What is an initialization list

The initialization list is for each member variabledefinitionThe place. Regardless of whether we have written it or not, member variables must go through the initialization list.
Initialization list: Begins with a colon, followed by a comma-separated list of data members, each "member variable" followed by an initial value or expression in parentheses.

#include<iostream>
using namespace std;
class A {
    
    
public:
	A()  //构造函数中的初始化列表
		:_a(10)
		,_b(20)
	{
    
    
		cout << "A()" << endl; 
	}
	void print()
	{
    
    
		cout << "_a = " << _a << endl;
		cout << "_b = " << _b << endl;

	}
private:
	int _a; // 注意,这里仅为声明,不是定义的地方。
	int _b;
};
int main()
{
    
    
	A a;
	a.print();
	return 0;
}

The console output is as follows:
Insert image description here
As can be seen from the above figure, the instantiated object a calls the constructor, and the values ​​in the initialization list are also assigned to the corresponding member variables.

But in this case, the value of the initialization list is not seen, because I can completely write the assignment form in the constructor, such as the following: the output result is the same.

A()  //构造函数
{
    
    
	_a = 10;
	_b = 20;
	cout << "A()" << endl; 
}

2. When to use initialization list

The following members appear in the class and must be placed in the initialization list (when defined) for initialization:
1. const member variables
2. Reference member variables
3. Custom type member variables (and no default constructor is available)

Take const member variables as an example:
Insert image description here

As mentioned before, the initialization list is where member variables are defined, and const assignment must be initialized where they are defined. Therefore, you can use the initialization list to assign values ​​to const variables:

#include<iostream>
using namespace std;
class A {
    
    
public:
	A()
		:_b(20)  //初始化列表初始化_b
		,_a(10)
	{
    
    
		cout << "A()" << endl; 
	}
	void print()
	{
    
    
		cout << "_a = " << _a << endl;
		cout << "_b = " << _b << endl;

	}
private:
	int _a;
	const int _b;   //const成员变量
};
int main()
{
    
    
	A a;
	a.print();
	return 0;
}

Assignment at declaration - default value

Sometimes you may encounter a situation where a value is assigned at the declaration: you must know thatThis is a declaration, not a definition. The definition is completed in the initialization list.. What is given here is actually the default value, just like the default parameter :
if the initialization list shows a value, the value of the initialization list is used.
If the initialization list does not show a value, the default value is used.

#include<iostream>
using namespace std;
class A {
    
    
public:
	A()
		:_a(100)  // 显示给值
	{
    
    
		cout << "A()" << endl; 
	}
	void print()
	{
    
    
		cout << "_a = " << _a << endl;
		cout << "_b = " << _b << endl;

	}
private:
    //必须知道,这里的是声明,不是定义。
	int _a = 10; //这里给的实际上是缺省值,就像缺省参数一样。
	const int _b = 20;  //const成员变量
};
int main()
{
    
    
	A a;
	a.print();
	return 0;
}

The console output is as follows: _a displays the value, outputs 100, _b does not, and outputs the default value 20
Insert image description here

3. Points to note

1. Each member variable can only appear once in the initialization list (initialization can only be initialized once)
2. The initialization list is initialized in the order in which the member variables are declared.
3. Again, if the following members appear in the class, they must be placed in the initialization list (when defined) for initialization:
const member variables, reference member variables, custom type member variables (and no default constructor is available)

BB at the end of the article: For friends who have any questions, feel free to leave a message in the comment area. If there are any questions, friends are welcome to point out in the comment area. The blogger will confirm the modification as soon as possible after seeing it. Finally, it is not easy to make. If it is helpful to friends, I hope to give the blogger some likes and attention.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_73390155/article/details/132342359