Initializer list
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
(为了不被混淆性病的初始)
他们的一部分,它负责成员和祖先初始化的构造函数
原文:
They are the part of a constructor which is responsible for member and ancestor initialization
[编辑] 语法
constructor_signature : member_constructor_calls { constructor_body }
|
|||||||||
[编辑] 解释
初始化列表中初始化的对象应该出现的地方,是基类的构造函数和成员被称为.
原文:
The initializer list is the place where initialization of the object should occur, there is where the constructors for base classes and members are called.
成员被初始化以相同的顺序,它们被声明,而不是像在初始化列表中出现.
原文:
Members are initialized in the same order as they are declared, not as they appear in the initializer list.
如果一个参数的构造函数中具有相同名称的成员之一,该标识符的模糊性,通过在构造函数中调用内的intializer列表选择参数(而不是成员)解决.
原文:
If a parameter in the constructor has the same name as one of the members, the ambiguity of that identifier being passed in a constructor call inside the intializer list is resolved choosing the parameter (and not the member).
成员或基类中不存在该列表中的缺省构造
原文:
Members or base classes not present in the list will be default constructed
[编辑] 为例
struct Class : public Base { int x; int y; Class ( int x ) : Base ( 123 ), // initialize base class x ( x ), // x (member) is initialized with x (parameter) y ( 0 ) // y initialized to 0 {} // empty constructor body Class ( double a ) : y ( a+1 ), x ( y ) // x will be initialized before y, this means that its value here is undefined {} // No base class constructor in list, this is the same as calling Base() Class() try : Base ( 789 ), x ( 0 ), y ( 0 ) { // no exception } catch (...) { // exception occurred on initialization } };