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;
}
// 应该在所有地方使用成员初始化列表进行初始化,要不然会浪费性能,对于整型这样的基本类型,它不会被初始化,除非你通过赋值来初始化它们
相关推荐
椰萝Yerosius5 小时前
[题解]2024CCPC郑州站——Z-order Curve
c++·算法
滨HI08 小时前
C++ opencv简化轮廓
开发语言·c++·opencv
学习路上_write8 小时前
FREERTOS_互斥量_创建和使用
c语言·开发语言·c++·stm32·单片机·嵌入式硬件
闻缺陷则喜何志丹9 小时前
【SOSDP模板 容斥原理 逆向思考】3757. 有效子序列的数量|分数未知
c++·算法·力扣·容斥原理·sosdp·逆向思考
BestOrNothing_201510 小时前
一篇搞懂 C++ 重载:函数重载 + 运算符重载,从入门到会用(含 ++、<<、== 实战)
c++·函数重载·运算符重载·operator·前置后置++·重载与重写
2501_9411444210 小时前
Python + C++ 异构微服务设计与优化
c++·python·微服务
程序猿编码10 小时前
PRINCE算法的密码生成器:原理与设计思路(C/C++代码实现)
c语言·网络·c++·算法·安全·prince
charlie11451419111 小时前
深入理解C/C++的编译链接技术6——A2:动态库设计基础之ABI设计接口
c语言·开发语言·c++·学习·动态库·函数
Cx330❀11 小时前
C++ STL set 完全指南:从基础用法到实战技巧
开发语言·数据结构·c++·算法·leetcode·面试
zmzb010311 小时前
C++课后习题训练记录Day33
开发语言·c++