Initializer list
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
(為了不被混淆性病的初始)
Original:
( Not to be confused with 性病的初始 )
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
他們的一部分,它負責成員和祖先初始化的構造函數
Original:
They are the part of a constructor which is responsible for member and ancestor initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 語法
constructor_signature : member_constructor_calls { constructor_body }
|
|||||||||
[编辑] 解釋
初始化列表中初始化的對象應該出現的地方,是基類的構造函數和成員被稱為.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
成員被初始化以相同的順序,它們被聲明,而不是像在初始化列表中出現.
Original:
Members are initialized in the same order as they are declared, not as they appear in the initializer list.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
如果一個參數的構造函數中具有相同名稱的成員之一,該標識符的模糊性,通過在構造函數中調用內的intializer列表選擇參數(而不是成員)解決.
Original:
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).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
成員或基類中不存在該列表中的預設構造
Original:
Members or base classes not present in the list will be default constructed
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 為例
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 } };

