C++的成员初始化列表

C++的成员构造函数初始化列表:构造函数中初始化类成员的一种方式,当我们编写一个类并向该类添加成员时,通常需要某种方式对这些成员变量进行初始化。

建议应该在所有地方使用成员初始化列表进行初始化

成员初始化的方法

方法一:

正常使用{}赋值类成员变量

cpp 复制代码
class Entity
{
private:
	string m_Name;  // 第一种方法会在这使用一次默认构造函数Entity()
	int m_Score;
	Example m_example;
public:
	//第一种方法初始化
	//默认构造函数
	Entity()
	{
		m_Name = "Unknown";  // 第一种方法在构造函数时会调用两次默认构造函数
	}
	//构造函数:接受name作为参数的构造函数
	Entity(const string& name)
	{
		m_Name = name;
	}
};

方法二:

使用成员初始化列表方式

形式就是函数名后面加上冒号,对成员变量进行赋值,用括号替换等号

cpp 复制代码
class Entity
{
private:
	string m_Name; 
	int m_Score;
	Example m_example;
public:
    Entity()
		: m_example(Example(8)) // m_example(8)等价于m_example(Example(8))
	{
		//m_example = Example(8);
	}
	// 成员初始化列表方式
	// 注意:不管你怎末写初始化列表,它都会按照定义类成员的顺序进行初始化,因此要确保做成员初始化列表时,要与成员变量声明时的顺序一致
	Entity(const int score)
		: m_Name("Unknown"),m_Score(score)  // 列出想要初始化的成员,等价m_Name = "Unknown";
	{
	}
	Entity(const string& Name,const int& score)
		: m_Name(Name),m_Score(score)  // 用括号替换等号,等价m_Name = name;
	{
	}
	const string& GetName() const
	{
		return m_Name;
	}
};

注意:不管你怎末写初始化列表,它都会按照定义类成员的顺序进行初始化,因此要确保做成员初始化列表时,要与成员变量声明时的顺序一致。成员变量声明时按照m_Name,m_Score顺序,使用成员初始化列表时需要同声明顺序一致。

两种方法的差别:

在特定类,两者功能上有区别

第一种方法在构造函数时会调用两次默认构造函数

案例:

cpp 复制代码
# include <iostream>
# include <string>
using namespace std;

class Example
{
public:
	Example()
	{
		cout << "Created Entity!" << endl;
	}
	Example(int x)
	{
		cout << "Created Entity with " << x << "!" << endl;
	}

};

class Entity
{
private:
	string m_Name;  // 第一种方法会在这使用一次默认构造函数Entity()
	int m_Score;
	Example m_example;
public:
	//在特定类,两者功能上有区别
	//第一种方法初始化
	//默认构造函数
	Entity()
	{
		m_Name = "Unknown";  // 第一种方法在构造函数时会调用两次默认构造函数
		m_example = Example(8);
	}
};
int main()
{
	Entity e0;
	//cout << e0.GetName() << endl;

	/*Entity e1("Chen");
	cout << e1.GetName() << endl;*/
	cin.get();
	return 0;
}

输出结果会同时打印出Created Entity! 以及 Created Entity with 8!。由于string是一个类,因此在声明变量时就会调用默认构造函数Entity(),而在实例化对象中,也会调用一次构造函数,然后丢弃第一次的字符串,造成资源的浪费。

当使用成员初始化列表方式就不会出现这种问题。

cpp 复制代码
Entity()
		: m_example(Example(8)) // m_example(8)等价于m_example(Example(8))
	{
		m_Name = "Unknown";  // 第一种方法在构造函数时会调用两次默认构造函数
		//m_example = Example(8);
	}

总的来说:应该在所有地方使用成员初始化列表进行初始化,要不然会浪费性能,对于整型这样的基本类型,它不会被初始化,除非你通过赋值来初始化它们

整体代码:

cpp 复制代码
# include <iostream>
# include <string>
using namespace std;

class Example
{
public:
	Example()
	{
		cout << "Created Entity!" << endl;
	}
	Example(int x)
	{
		cout << "Created Entity with " << x << "!" << endl;
	}

};

class Entity
{
private:
	string m_Name;  // 第一种方法会在这使用一次默认构造函数Entity()
	int m_Score;
	Example m_example;
public:
	//在特定类,两者功能上有区别
	//第一种方法初始化
	//默认构造函数
	Entity()
		: m_example(Example(8)) // m_example(8)等价于m_example(Example(8))
	{
		m_Name = "Unknown";  // 第一种方法在构造函数时会调用两次默认构造函数
		//m_example = Example(8);
	}
	//构造函数:接受name作为参数的构造函数
	Entity(const string& name)
	{
		m_Name = name;
	}
	// 成员初始化列表方式
	// 注意:不管你怎末写初始化列表,它都会按照定义类成员的顺序进行初始化,因此要确保做成员初始化列表时,要与成员变量声明时的顺序一致
	Entity(const int score)
		: m_Name("Unknown"),m_Score(score) // 列出想要初始化的成员,等价m_Name = "Unknown";
	{
	}
	Entity(const string& Name,const int& score)
		: m_Name(Name),m_Score(score)  // 用括号替换等号,等价m_Name = name;
	{
	}
	const string& GetName() const
	{
		return m_Name;
	}
};
int main()
{
	Entity e0;
	//cout << e0.GetName() << endl;

	/*Entity e1("Chen");
	cout << e1.GetName() << endl;*/
	cin.get();
	return 0;
}
// 应该在所有地方使用成员初始化列表进行初始化,要不然会浪费性能,对于整型这样的基本类型,它不会被初始化,除非你通过赋值来初始化它们
相关推荐
ShineWinsu9 小时前
对于Redis:Hash类型的解析
c++·redis·分布式·缓存·面试·hash·哈希表
wuminyu9 小时前
JEP491中synchronized关键字引发的平台线程钉住解决方法简介
java·linux·c语言·jvm·c++
All for pursuit.9 小时前
【贪心-4】581.最短无序连续子数组
数据结构·c++·算法·leetcode
无名猿10 小时前
list 与 forward_list:链表真的比 vector 快吗
c++·性能优化·stl·内存管理·标准库
笑鸿的学习笔记10 小时前
C++笔记之SIMD与SSE指令集
开发语言·c++·笔记
汉克老师10 小时前
GESP2026年9月认证C++一级( 第二部分判断题(1~10题)精讲
c++·gesp·小学生·学c++编程
一只旭宝11 小时前
C++ 后端开发面试题整理
java·c++·面试
xlq2232211 小时前
全面复习1
c++
Dylan的码园11 小时前
求平均成绩 矩阵数据处理 【循环处理 二维数组】
c++·线性代数·矩阵
All for pursuit.12 小时前
【回溯-2】22.括号生成
数据结构·c++·算法·leetcode