One article to understand the initialization list of the C++ constructor

problem introduction

Read the code below:

class Time
{
    
    
public:
	Time(int hour, int min, int sec)
	{
    
    
		_hour = hour;
		_min = min;
		_sec = sec;
	}
private:
	int _hour;
	int _min;
	int _sec;
};

After we instantiate the object, the object automatically calls the constructor to initialize its own value and complete the initialization work.But we can't call it initialization of member variablesBecause the initialization can only be initialized once, but the "initialization" work in the function body, to be precise, it can be assigned multiple times, so it is biased to regard the assignment of the function body as initialization.
Hence the introduction of the initializer list

1 initialization list

Syntax: 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 specific format is as follows:

class Time
{
    
    
public:
	Time(int hour, int min, int sec)
		: _hour(hour)
		, _min(min)
		, _sec(sec)
	{
    
    }


private:
	int _hour;
	int _min;
	int _sec;
};

2 Precautions

When the object is initialized, the member variables will be initialized according to the initialization list (if there is a custom type, the corresponding default constructor will be called), and then the content of the function body will be executed. C++ knows how to initialize member variables based on the initialization list. If the initialization list is not displayed, the function will generate the corresponding initialization list by default.

  1. Each member variable can only appear once in the initialization list (initialization can only appear once).
  2. The class contains reference member variables, const member variables, and custom type members (and when the class has no default constructor), they must be placed in the initialization list for initialization.
  3. 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.

1~3

  1. Each member variable in the initializer listcan onlyOccurs once (initialization can only occur once).
  2. class containsReference member variables, const member variables, custom type members (and when the class has no default constructor), they must be initialized by placing them in the initializer list.

If you want to initialize a custom type, you must first ensure that there is a default constructor for the type (note what is the default constructor), if not, an error will be reported, because if the current class does not have a corresponding constructor, the compiler will call the default constructor of the class function to execute. code show as below:

For custom types

class Date 
{
    
    
public:

	// 重点在构造函数,其他不是重点
	// 全缺省才是默认构造函数
	//Date(int year=0, int month=0, int day=0)
	//{
    
    
	//	_year = year;
	//	_month = month;
	//	_day = day;
	//}

	Date&  operator=(const Date& d1)
	{
    
    
		_year = d1._year;
		_month = d1._month;
		_day = d1._day;
		return *this;
	}
private:
	int _year;
	int _month;
	int _day;
};
class Time
{
    
    
public:
	Time(int hour=0, int min=0, int sec=0)
		: _hour(hour)
		, _min(min)
		, _sec(sec)
	{
    
    
	//在函数体初始化,比较麻烦,必须保证存在默认构造z函数
		Date t1;
		_d = t1;
	}


private:
	int _hour;
	int _min;
	int _sec;
	Date _d;
};


int main()
{
    
    
	Time t(6, 30, 30);
	return 0;
}

insert image description here

If the default constructor of the Date class is not displayed, the compiler will not initialize the value of _d, which is a random value. On the contrary, if the initialization work is written in the initialization list, even if the default constructor is not displayed in the Date class, the compiler will initialize the custom type of _d to 0.

class Date
{
    
    
public:
	Date& operator=(const Date& d1)
	{
    
    
		_year = d1._year;
		_month = d1._month;
		_day = d1._day;
		return *this;
	}
private:
	int _year;
	int _month;
	int _day;
};

class Time
{
    
    
public:
	Time(int hour = 0, int min = 0, int sec = 0)
		: _hour(hour)
		, _min(min)
		, _sec(sec)
		,_d()
	{
    
    
	}

private:
	int _hour;
	int _min;
	int _sec;
	Date _d;
};


int main()
{
    
    
	Time t(6, 30, 30);
	return 0;
}

insert image description here


For the const type
const modified member variable is a constant, it cannot be assigned in the function body, because the constant cannot be modified. But you can initialize it in the list, or use the features of C++11 to assign a value at the declaration.

insert image description here


For reference types:
reference types, the reference must be initialized when it is defined, and cannot be reassigned, so it must also be written in the initialization list.

insert image description here

This blog explains more in detail, please refer to


  1. 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.

Guess you like

Origin blog.csdn.net/weixin_45153969/article/details/132686616