[C++] Constructor initialization list ③ (Constructor initialization list is initialization of const member variables)


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;




1. Initialization of const member variables in the initialization list of the constructor




1. Initialize const constant members


If a member variable modified by const is defined in the class, the member variable must be initialized, otherwise an error will be reported;

Const members in an object must be initialized immediately after declaration;

Initialization of const members can only be initialized through the initialization list of the constructor;

Note: A distinction is made between initialization and assignment here.

  • Initialization means setting an initialization value for a variable when it is declared;
  • Assignment is to assign a value to the variable after it is declared;

Const member variables are constants and cannot be assigned after declaration, so the const member variables must be assigned in the initialization list of the constructor;


2. Error code example - constant member not initialized


In the following class B, the constant const int m_const_int is defined;

If the constant is not initialized, an error will be reported when compiling and running;

class B
{
    
    
public:
	B() : m_age(10), m_a(10, 150)
	{
    
    }

	// 构造函数中的参数可以作为 参数列表 中的参数值
	B(int age, int ageOfA, int heightOfA) : m_age(age), m_a(ageOfA, heightOfA)
	{
    
    
		cout << "执行 B 的构造函数" << endl;
	}

	~B()
	{
    
    
		cout << "执行 B 的析构函数" << endl;
	}
public:
	int m_age;		// 年龄
	A m_a;			// A 类型成员变量
	const int m_const_int;	// 常量成员
};

Results of the :

已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(29,2): error C2789: “B::m_const_int”: 必须初始化常量限定类型的对象
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(44): message : 参见“B::m_const_int”的声明
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(33,2): error C2789: “B::m_const_int”: 必须初始化常量限定类型的对象
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(44): message : 参见“B::m_const_int”的声明
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

During compilation, errors will be reported in all constructors;

In all constructors, constant members must be initialized in the initialization list;

Insert image description here


3. Correct code example - initializing constant members in the initialization list


In the following class B, in all constructors, the initialization list must be used to initialize the constant members. As long as a constructor is omitted and the constant members are not initialized, an error will be reported;


Code example:

class B
{
    
    
public:
	B() : m_age(10), m_a(10, 150), m_const_int(888)
	{
    
    }

	// 构造函数中的参数可以作为 参数列表 中的参数值
	B(int age, int ageOfA, int heightOfA) : m_age(age), m_a(ageOfA, heightOfA), m_const_int(888)
	{
    
    
		cout << "执行 B 的构造函数" << endl;
	}

	~B()
	{
    
    
		cout << "执行 B 的析构函数" << endl;
	}
public:
	int m_age;		// 年龄
	A m_a;			// A 类型成员变量
	const int m_const_int;	// 常量成员
};

Results of the :

Insert image description here


4. Complete code example


Complete code example:

#include "iostream"
using namespace std;

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

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

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

class B
{
    
    
public:
	B() : m_age(10), m_a(10, 150), m_const_int(888)
	{
    
    }

	// 构造函数中的参数可以作为 参数列表 中的参数值
	B(int age, int ageOfA, int heightOfA) : m_age(age), m_a(ageOfA, heightOfA), m_const_int(888)
	{
    
    
		cout << "执行 B 的构造函数" << endl;
	}

	~B()
	{
    
    
		cout << "执行 B 的析构函数" << endl;
	}
public:
	int m_age;		// 年龄
	A m_a;			// A 类型成员变量
	const int m_const_int;	// 常量成员
};


int main()
{
    
    
	// 通过 B 的有参构造函数
	// 其中 构造函数中的参数 作为 参数列表 中的参数值
	B b(10, 10, 150);


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

Results of the :

执行 A 的构造函数
执行 B 的构造函数
Press any key to continue . . .
执行 B 的析构函数
执行 A 的析构函数

D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\Debug\HelloWorld.exe (进程 20756)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

Insert image description here

Guess you like

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