一个
初始化列表
是一个列表,
括号
(
{}
)
初始化
,
可以用来初始化
简单
聚合数据类型
和
类实现
std:
:
initializer_list
:
Uniform initialization
As noted above, C++03 is inconsistent in how it lets you initialize different types of data. Initializer lists go a long way to helping making initialization of data more consistent. However, C++11 has one more trick up its sleeve called uniform initialization. Unlike initializer lists, which take the form:
1
|
type variable = { data, elements };
|
The uniform initialization syntax takes the following form:
1
|
type variable { data, elements };
// note: no assignment operator
|
This style of initialization will work for both plain aggregate data types (structs and C-style arrays) and classes. For classes, the following rules are observed:
- If there is an initialization_list constructor of the appropriate type, that constructor is used
- Otherwise the class elements are initialized using the appropriate constructor