C++ - value of uninitialized vector

后端 未结 5 1524
情歌与酒
情歌与酒 2020-12-03 09:28

I understand from the answer to this question that values of global/static uninitialized int will be 0. The answer to this one says that for vectors, the default construc

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 10:10

    By default, vector elements are zero-initialized and not default-initialized. Those are two different but related concepts:

    • zero-initialization is what is done for static objects not having an explicit initialization and what is done for a member given in the initialized list with an initializer of (). For basic types, the value used is 0 converted to the type.

    • default-initialization is what is done for not explicitly initialized non static variables and members. For basic types it stay uninitialized.

    (And C++0X introduces value-initialization which is still different).

提交回复
热议问题