Classes and Objects [3] Initialization Lists

introduction

The constructor was introduced in the previous article, which can be automatically called when instantiating a class object to initialize the class object: poke
me to see the detailed explanation of the default member function
. However, it is not difficult to find that the member variables in the constructor body Initialization is actually an assignment to them rather than a definition. Then for some variables that can only be given a value at the time of definition, they cannot be changed after definition (such as const variables and reference variables) or some class type member variables without a default constructor, they cannot be defined in the constructor. Using the constructor obviously cannot meet our needs.

So where are these member variables defined when the class object is instantiated? How are they initialized when they are defined?
Member variables are defined and initialized in an initializer list :

initialization list

General member variables can be assigned multiple times, and of course it is possible to assign values ​​in the constructor body. However, in the constructor body, only assignment can be realized and member variables cannot be defined, so const member variables, reference member variables, and class members without default constructors cannot be defined. And these member variables must be explicitly defined and initialized in the initialization list:

class A
{
    
    
public:
	A(int a)
	{
    
    
		_a = a;
	}
private:
	int _a;
};

class B
{
    
    
public:
	/* B(int b)
	{
		_b = b; 
		_c = b;  
	} */
	//错误代码: 表达式不能是常量,A不能没有默认构造函数
private:
	int& _b;       //引用成员变量
	const int _c;  //const成员变量
	A _obj;        //没有默认构造函数的成员类
};

insert image description here

definition

The initializer list comes after the constructor parameter list and begins with a colon, followed by a comma-separated list of data members, each "member variable" followed by an initial value or expression enclosed in parentheses:

类名(参数列表)
   : 成员变量(初始值)
   , 成员变量(初始值)
   , ...
{
    
    }

The initialization list is where the member variables are defined, and we can define and initialize the above three member variables in the initialization list:

class A
{
    
    
public:
	A(int a)
	{
    
    
		_a = a;
	}
private:
	int _a;
};

class B
{
    
    
public:
	B(int b)
		: _b(b)
		, _c(b)
		, _obj(b) //在初始化列表调用其构造函数
	{
    
    }
private:
	int& _b;
	const int _c;
	A _obj;
};

characteristic

  1. Each member variable can only appear once in the initialization list (initialization can only be initialized once);
  2. Reference member variables, const member variables, and member classes without default member functions must be placed in the initialization list for initialization ;
  3. For custom type member variables, the initialization list will be used to define and initialize first (the default value given in the member variable declaration is used in the initialization list);
  4. The order in which member variables are declared in the class is the order in which they are initialized in the initialization list, regardless of their order in the initialization list (so it is recommended that the order of declaration be consistent with the order in the initialization list):
class A
{
    
    
public:
	A(int a)
		: _a1(a)
		, _a2(a1) //错误代码:未定义标识符a1
	{
    
    }

private:
	int _a2;
	int _a1;
};

Summarize

At this point, the introduction of the initialization list is over.
In future programming, it is recommended that member variables be initialized in the initialization list. Of course, there are also some situations that need to be assigned in the constructor. For example, when it is necessary to dynamically open up space, the initialization list is obviously not suitable.

If you think that I did not introduce a certain part clearly or that there is a problem with a certain part, you are welcome to raise it in the comment area

If this article is helpful to you, I hope it will be connected with one click

Hope to make progress together with you

Guess you like

Origin blog.csdn.net/weixin_73450183/article/details/130756392