c++ - 默认初始化和值初始化之间的区别?

标签 c++ initialization

Possible Duplicate:
What do the following phrases mean in C++: zero-, default- and value-initialization?

我正在阅读 this answer ,所以我遇到了第二个词:value-initialize。最初我认为这与 default-initialize 相同,但上下文提示我错了。

所以我的问题是:

default-initializevalue-initialize有什么区别?

我想通过一些例子来了解其中的区别。

最佳答案

根据标准(8.5/4,5):

To default-initialize an object of type T means:
— if T is a non-POD class type 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;96)
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

关于c++ - 默认初始化和值初始化之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5610629/

相关文章:

c++ - 不打电话就工作的过分热心的方法? C++ Glut初始化变得流氓

c++ - QPushButton 和 QToolButton 的区别

c++ - 如何在函数中初始化 CString& 参数

c++ - 为什么我可以分配一个长度小于数组本身的字符串文字?

c++ - 如何将 UCHAR 转换为二进制

java - 将 String 转换为 int 数组

javascript - 如何在 JQueryMobile 中使用 autoInitializePage 与客户端生成的内容?

c++ - 我可以通过 WiX 的自定义操作访问 .wxl 本地化文件中的字符串吗?

c++ - std::map 使用 STL::map 旋转方法或算法

c++ - 用于私有(private)、 protected 和公共(public)元素的 Doxygen 自定义 CSS