C++相談室 part123 [無断転載禁止]©5ch.io
■ このスレッドは過去ログ倉庫に格納されています
0043デフォルトの名無しさん
2016/02/22(月) 23:33:38.04ID:/YQF/sqYn3690から抜粋すると
8.5.17:The semantics of initializers are as follows. The destination type is the type of the object or reference being
initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly
parenthesized) expression, the source type is not defined.
— If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized
8.5.4.3: List-initialization of an object or reference of type T is defined as follows:
if the initializer list has no elements and T is a class type with a default constructor, the
object is value-initialized.
8.5.8: To value-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a
default constructor that is user-provided or deleted, then the object is default-initialized
8.5.7: To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor (12.1) for T is called
つまりこの場合に限ると
・リストで初期化するとlist-initializeされる。
・リストが空の場合、list-initializeはvalue-initializeになる。
・デフォルトコンストラクタが削除されてたり、ユーザ定義されてる場合はvalue-initializeはdefault-initializeになる。
・default-initializeはデフォルトコンストラクタを呼び出す
・hoge::hoge()は何も実行しない→hoge::bakaは未定義のままになる
といった具合。
解決するには、
hoge() : baka(0) {return;}
みたいにちゃんと初期化コードを書かなきゃ駄目。
■ このスレッドは過去ログ倉庫に格納されています