Uniform initialization

来源:互联网 发布:360软件清理大师 编辑:程序博客网 时间:2024/05/17 07:25
一个初始化列表是一个列表,括号{}初始化可以用来初始化简单聚合数据类型类实现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

0 0
原创粉丝点击