gpt4 book ai didi

c++ - 以下短语在 C++ : zero-, 默认和值初始化中是什么意思?

转载 作者:IT老高 更新时间:2023-10-28 11:28:50 25 4
gpt4 key购买 nike

以下短语在 C++ 中的含义:

  • 零初始化,

  • 默认初始化,以及

  • 值初始化

C++ 开发人员应该对它们了解多少?

最佳答案

C++03 标准 8.5/5:

To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.

To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.

To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

A program that calls for default-initialization or value-initialization of an entity of reference type is ill-formed. If T is a cv-qualified type, the cv-unqualified version of T is used for these definitions of zero-initialization, default-initialization, and value-initialization.

关于c++ - 以下短语在 C++ : zero-, 默认和值初始化中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1613341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com