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

前端 未结 6 1968
无人及你
无人及你 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:35

    Native types like int usually get a garbage value, eg. whatever happens to reside in the memory area it is created in. However this is not defined in the standard, and it might also be initialized to 0, which is quite common in eg. debug builds.

    EDIT. But basically, you should never trust an uninitialized variable to hold something specific; Always define the values yourself.

提交回复
热议问题