8
class A {
public:
   A();

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

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

Is this right?

unwind
  • 391,730
  • 64
  • 469
  • 606
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86

2 Answers2

18

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).

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

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

And from §8.5/5:

To value-initialize an object of type T means:

  • 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);
  • 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

To zero-initialize an object of type T means:

  • if T is a scalar type, 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.

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.

Community
  • 1
  • 1
ildjarn
  • 62,044
  • 9
  • 127
  • 211
3

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

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:

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

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

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)};
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Wait, not in C++0x either? D: – GManNickG Apr 09 '11 at 00:42
  • Well, in C++0x it is understood (I hope!) that you should always use `std::array<>` over C-arrays, which can be sensibly and cheaply initialized and returned from a static member function for use in c'tor initialization lists. – ildjarn Apr 09 '11 at 00:46
  • @GMan: Indeed. You can't initialise an array with an `std::initializer_list<>`, because arrays have no constructors. You still end up iterating through _something_ in the constructor body of the enclosing type. The reason that this is not really a problem is what ildjarn said. – Lightness Races in Orbit Apr 09 '11 at 01:55
  • g++ 4.5.2 has no problem with `A() : a{1, 2, 3, 4, 5} { }` (That doesn't necessarily mean that it's allowed in C++0x, of course.) – James McNellis Apr 09 '11 at 01:59
  • @JamesMcNellis: Now that you mention it, it may be that I was originally thinking of the `std::initializer_list<>` case by accident; basic element-wise initialisation should indeed be valid, shouldn't it? – Lightness Races in Orbit Apr 09 '11 at 02:07
  • I'll be honest: I know practically nothing about initializer lists since Visual C++ doesn't support them. – James McNellis Apr 09 '11 at 02:13
  • `std::array<>` is an aggregate. It has no constructors beyond default/copy and (potentially) move (and therefor also no initializer-list constructor). Note that you *could* write a constructor like `template A(T... t):x{t...} { }` and then write `A a{1, 2, 3}`, but this looks like it would create lots of little template instantiations. Not sure about the code bloat it causes. See http://stackoverflow.com/questions/4118025/brace-enclosed-initializer-list-constructor/4118050#4118050 – Johannes Schaub - litb Apr 09 '11 at 15:03