C++11中列表初始化机制的概念是什么

发布时间:2021-11-09 16:32:20 作者:iii
来源:亿速云 阅读:134

本篇内容介绍了“C++11中列表初始化机制的概念是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

概述

定义:列表初始化是C++11引入的新标准,目的是统一初始化方式

C++11以前只能使用列表初始化来初始化内置类型数组和POD类型对象,C++11中列表初始化可以用于初始化任何类型对象

注意:区分列表初始化和初始化列表

        列表初始化:用{}进行初始化的方式

        初始化列表:构造函数体前对对象成员直接进行初始化的列表

        initializer_list:一种用于未定参数的轻量STL容器

实现机制详解

对内置类型对象、POD对象和类对象的列表初始化实现细节是不同的

POD类型的列表初始化

含有构造函数的类的列表初始化(C++11)

列表初始化用于函数返回值

引入std::initializer_list

代码验证

class testClass
{
private:
	int a;
	int b;
public:
	testClass() :a(0), b(0) {
		cout << "default init\n";
	}
	testClass(int a) :a(a), b(a) {
		cout << "sing-val init\n";
	}
	testClass(int a, int b) :a(a), b(b) {
		cout << "val init\n";
	}
	testClass(testClass& temp) :a(temp.a), b(temp.b) {
		cout << "copy init\n";
	}
	testClass& operator=(testClass& temp) {
		//testClass& newobj = *this;
		a = temp.a;
		b = temp.b;
		cout << "copy assign\n";
		return *this;
	}
	testClass& operator=(int x) {
		a = x;
		b = x;
		cout << "int-convert assign\n";
		//testClass& newobj = *this;
		return *this;
	}
	testClass& operator++() {
		a++;
		b++;
	}
	void printVal(ostream& os) {
		os << "a=" << a << "\n";
		os << "b=" << b << "\n";
	}
};
using tc = testClass;
tc& makeObj(int x, int y)
{
	return { x,y };
}
int main()
{
	tc a(1, 1); //val init
	tc b{ 1,1 }; //val init
	tc c = { 1,1 }; //val init
	tc d = tc{ 1,1 }; //val init
	cout << endl;
	tc* e = new tc[2]; //default init *2
	cout << endl;
	tc* f = new tc[3]{ {1,1},{2,2},{3,3} }; //val init *3
	cout << endl;
	tc* g = new tc[5]{ {1,1},{1} }; // val init + sing-val init + default init *3
	cout << endl;
	cout << "testing return val of init_list\n";
	tc h = makeObj(2, 2); //val init
	tc i = h; //copy init
	i = d; //copy assign
	i.printVal(cout);
	return 0;
}

列表初始化测试

添加initializer_list为参数的构造函数后

testClass::testClass(initializer_list<int> list) :a(0), b(0)
{
	int ab = 1;
	for (auto it = list.begin(); it != list.end(); it++)
	{
		if (ab)
			a += *it;
		else
			b += *it;
	}
	cout << "init_list init\n";
}
 
int main()
{
	tc a(1, 1); //val init
	tc b{ 1,1 }; //val init
	tc c = { 1,1 }; //val init
	tc d = tc{ 1,1 }; //val init
	cout << endl;
	tc* e = new tc[2]; //default init *2
	cout << endl;
	tc* f = new tc[3]{ {1,1},{2,2},{3,3} }; //val init *3
	cout << endl;
	tc* g = new tc[5]{ {1,1},{1} }; // val init + sing-val init + default init *3
	cout << endl;
	cout << "testing return val of init_list\n";
	tc h = makeObj(2, 2); //val init
	tc i = h; //copy init
	i = d; //copy assign
	i.printVal(cout);
	cout << endl;
	cout << "testing argument init_list\n";
	tc j = { 1,2,3,4,5,6 };
	tc k = { 9 };
	return 0;
}

以下为运行截图

C++11中列表初始化机制的概念是什么添加init_list后测试截图

由此可见所有列表初始化都调用了含有initializer_list为参数的构造函数,证实了列表初始化是基于隐式转换并以initializer_list为底层实现的构想

应用

列表初始化防止类型收窄

C++11的列表初始化还有一个额外的功能就是可以防止类型收窄,也就是C++98/03中的隐式类型转换,将范围大的转换为范围小的表示,在C++98/03中类型收窄并不会编译出错,而在C++11中,使用列表初始化的类型收窄编译将会报错:

int a = 1.1; //OK
int b{ 1.1 }; //error
 
float f1 = 1e40; //OK
float f2{ 1e40 }; //error
 
const int x = 1024, y = 1;
char c = x; //OK
char d{ x };//error
char e = y;//error
char f{ y };//error

“C++11中列表初始化机制的概念是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. bash中初始化机制的示例分析
  2. 总结C++11中decltype、类内初始化、列表初始化返回值

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:cnzz统计代码引起的Bad Request - Request Too Long的原因分析

下一篇:Django中的unittest应用是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》