如何使用成员初始化列表初始化数组?

时间:2022-03-07 19:31:37
class A {
public:
   A();

private:
   char a[5];
   int* ptr;
};

A::A() : a(0), ptr(0) { }

Is this right?

这是正确的吗?

2 个解决方案

#1


19  

The only sensible thing you can do with a C-array in C++03 is value-initialize it (in C++11 and beyond it can be list-initialized).

对于c++ 03中的C数组,惟一明智的做法是对它进行值初始化(在c++ 11中,甚至在它之外,可以对它进行列表初始化)。

From the C++03 standard, §8.5/7:

从c++ 03标准,§8.5/7:

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

初始化器为空圆括号集的对象,例如,(),应初始化值。

And from §8.5/5:

和§8.5/5:

To value-initialize an object of type T means:

值初始化类型为T的对象意味着:

  • if T is a class type with a user-declared constructor, then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • 如果T是一个具有用户声明的构造函数的类类型,则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是病态的);
  • 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;
  • 如果T是没有用户声明的构造函数的非联合类类型,则T的每个非静态数据成员和基类组件都是值初始化的;
  • if T is an array type, then each element is value-initialized;
  • 如果T是一个数组类型,那么每个元素都是值初始化的;
  • otherwise, the object is zero-initialized
  • 否则,对象是零初始化的

To zero-initialize an object of type T means:

零初始化类型为T的对象意味着:

  • if T is a scalar type, the object is set to the value of 0 (zero) converted to T;
  • 如果T是标量类型,则将对象设置为转换为T的0 (0);
  • if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
  • 如果T是一个非union类类型,则每个非静态数据成员和每个基类subobject都是零初始化的;
  • if T is a union type, the object’s first named data member) is zero-initialized;
  • 如果T是联合类型,则对象的第一个命名数据成员)为零初始化;
  • if T is an array type, each element is zero-initialized;
  • 如果T是数组类型,则每个元素都是零初始化的;
  • if T is a reference type, no initialization is performed.
  • 如果T是引用类型,则不执行初始化。

So, if your constructor definition is changed to

因此,如果您的构造函数定义被更改为

A::A() : a(), ptr() { }

then you are guaranteed that post-construction, all 5 elements of A::a will have the value '\0' and A::ptr will be null.

然后,您被保证在构建后,A:: A的所有5个元素都将具有值'\0'和A::ptr将为空。

#2


3  

Afraid not; C++ doesn't support initialising arrays like this.

不害怕;c++不支持像这样初始化数组。

You'll just have to assign to its members in A's constructor body, or you can use value-initialisation if you don't really care what the values are:

你只需要在A的构造函数体中分配它的成员,或者如果你不关心值是什么,你可以使用值初始化:

struct A {
   int x[5];
   A() : x();
};

C++0x does let you give all the values, though:

但是,c++ 0x允许您给出所有的值:

struct A {
   int x[5];
   A() : x{1,2,3,4,5} {}
};

Note, though, that because arrays are not class-objects, you won't be able to do this:

但是请注意,因为数组不是类对象,所以您不能这样做:

struct A {
   int x[5];
   A(std::initializer_list<int[5]>& i) // or whatever the T should be
      : x{i} // or x(i)
        {}
}
A a({1,2,3,4,5)};

#1


19  

The only sensible thing you can do with a C-array in C++03 is value-initialize it (in C++11 and beyond it can be list-initialized).

对于c++ 03中的C数组,惟一明智的做法是对它进行值初始化(在c++ 11中,甚至在它之外,可以对它进行列表初始化)。

From the C++03 standard, §8.5/7:

从c++ 03标准,§8.5/7:

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

初始化器为空圆括号集的对象,例如,(),应初始化值。

And from §8.5/5:

和§8.5/5:

To value-initialize an object of type T means:

值初始化类型为T的对象意味着:

  • if T is a class type with a user-declared constructor, then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • 如果T是一个具有用户声明的构造函数的类类型,则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是病态的);
  • 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;
  • 如果T是没有用户声明的构造函数的非联合类类型,则T的每个非静态数据成员和基类组件都是值初始化的;
  • if T is an array type, then each element is value-initialized;
  • 如果T是一个数组类型,那么每个元素都是值初始化的;
  • otherwise, the object is zero-initialized
  • 否则,对象是零初始化的

To zero-initialize an object of type T means:

零初始化类型为T的对象意味着:

  • if T is a scalar type, the object is set to the value of 0 (zero) converted to T;
  • 如果T是标量类型,则将对象设置为转换为T的0 (0);
  • if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
  • 如果T是一个非union类类型,则每个非静态数据成员和每个基类subobject都是零初始化的;
  • if T is a union type, the object’s first named data member) is zero-initialized;
  • 如果T是联合类型,则对象的第一个命名数据成员)为零初始化;
  • if T is an array type, each element is zero-initialized;
  • 如果T是数组类型,则每个元素都是零初始化的;
  • if T is a reference type, no initialization is performed.
  • 如果T是引用类型,则不执行初始化。

So, if your constructor definition is changed to

因此,如果您的构造函数定义被更改为

A::A() : a(), ptr() { }

then you are guaranteed that post-construction, all 5 elements of A::a will have the value '\0' and A::ptr will be null.

然后,您被保证在构建后,A:: A的所有5个元素都将具有值'\0'和A::ptr将为空。

#2


3  

Afraid not; C++ doesn't support initialising arrays like this.

不害怕;c++不支持像这样初始化数组。

You'll just have to assign to its members in A's constructor body, or you can use value-initialisation if you don't really care what the values are:

你只需要在A的构造函数体中分配它的成员,或者如果你不关心值是什么,你可以使用值初始化:

struct A {
   int x[5];
   A() : x();
};

C++0x does let you give all the values, though:

但是,c++ 0x允许您给出所有的值:

struct A {
   int x[5];
   A() : x{1,2,3,4,5} {}
};

Note, though, that because arrays are not class-objects, you won't be able to do this:

但是请注意,因为数组不是类对象,所以您不能这样做:

struct A {
   int x[5];
   A(std::initializer_list<int[5]>& i) // or whatever the T should be
      : x{i} // or x(i)
        {}
}
A a({1,2,3,4,5)};