What are primitive types default-initialized to in C++?

前端 未结 6 1975
无人及你
无人及你 2020-11-27 14:05

When I use an initialization list:

struct Struct {
    Struct() : memberVariable() {}
    int memberVariable;
};

the primitive type (

6条回答
  •  情书的邮戳
    2020-11-27 14:42

    For primitive types, default initialisation means that the object is initialised with 0, 0.0 or NULL as appropriate for the type.

    Edit: The above is valid for C++98. In C++03, the terms got redefined a bit. Now, using an initialiser of () (which is syntactically only possible for member-objects) results in value initialisation, which for primitive types means that the appropriate value of 0, 0.0 or NULL gets stored.

提交回复
热议问题