gpt4 book ai didi

c++ - 在此初始化方法中将整个数组设置为 NULL 是 C++ 标准吗?

转载 作者:行者123 更新时间:2023-11-30 00:55:03 25 4
gpt4 key购买 nike

class Foo {};

int main()
{
Foo *foo[500] = { NULL};
}

无论操作系统/编译器如何,整个数组都将设置为 NULL 是标准的吗?

最佳答案

是的,它是有效的并受 C++ 标准的保证。

引用:

C++03 标准 8.5.1 聚合
第 7 段:

If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized (8.5). [Example:

 struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };

initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is,0. ]

值初始化定义在:

C++03 8.5 初始化器
第 5 段:

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

关于c++ - 在此初始化方法中将整个数组设置为 NULL 是 C++ 标准吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13097781/

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