int* arr1 = {}; //ok: arr1 == NULL
int* arr2 = {1,2,3};//error: scalar object requires one element in initializer
我希望初始化arr1
会导致相同的错误,但事实并非如此。这是为什么?
编辑:
感谢@krzaq的答案。我的确对您的谈话感到困惑。
因此,以下陈述都是合法的:
int* arr1 = {};
int arr2[] = {1,2,3};
int* arr3 = {arr2};
您将自己与错误的命名混淆了。arr1
并且arr2
是指针,而不是数组。您可以对指针进行零初始化,并使用与完全相同的语法来完成此操作arr1
。您不能使用来初始化指针std::initializer_list<int>
。
如果这仍然使您感到困惑,请尝试对某些抽象类型进行推理:
using some_type = int*;
some_type foo = {}; // ok, value-initialized
some_type bar = {1,2,3}; // not ok, doesn't make sense to initialize
// bar with a list of ints
some_type baz = "a string"; // also not ok, doesn't make sense
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句