Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a member array in constructor initializer

class C  { public:  C() : arr({1,2,3}) //doesn't compile {}     /*     C() : arr{1,2,3} //doesn't compile either {}     */ private:  int arr[3]; }; 

I believe the reason is that arrays can be initialized only with = syntax, that is:

int arr[3] = {1,3,4}; 

Questions

  1. How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible?
  2. Does the C++03 standard say anything special about initializing aggregates (including arrays) in ctor initializers? Or the invalidness of the above code is a corollary of some other rules?
  3. Do C++0x initializer lists solve the problem?

P.S. Please do not mention vectors, boost::arrays, and their superiority to arrays, which I am well aware of.

like image 287
Armen Tsirunyan Avatar asked Oct 30 '10 08:10

Armen Tsirunyan


People also ask

Can we initialize array in constructor?

Initialize Array in Constructor in JavaWe can create an array in constructor as well to avoid the two-step process of declaration and initialization. It will do the task in a single statement. See, in this example, we created an array inside the constructor and accessed it simultaneously to display the array elements.

How do you initialize a data member in a constructor?

5) When constructor's parameter name is same as data member If constructor's parameter name is same as data member name then the data member must be initialized either using this pointer or Initializer List. In the following example, both member name and parameter name for A() is “i”.

How do you initialize an array in a constructor in Java?

One way to initialize the array of objects is by using the constructors. When you create actual objects, you can assign initial values to each of the objects by passing values to the constructor. You can also have a separate member method in a class that will assign data to the objects.

How do you declare an array in a constructor in CPP?

If no default constructor is defined for the class, the initializer list must be complete, that is, there must be one initializer for each element in the array. The first element of aPoint is constructed using the constructor Point( int, int ) ; the remaining two elements are constructed using the default constructor.


2 Answers

  1. How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible?

Yes. It's using a struct that contains an array. You say you already know about that, but then I don't understand the question. That way, you do initialize an array in the constructor, without assignments in the body. This is what boost::array does.

Does the C++03 standard say anything special about initializing aggregates (including arrays) in ctor initializers? Or the invalidness of the above code is a corollary of some other rules?

A mem-initializer uses direct initialization. And the rules of clause 8 forbid this kind of thing. I'm not exactly sure about the following case, but some compilers do allow it.

struct A {   char foo[6];   A():foo("hello") { } /* valid? */ }; 

See this GCC PR for further details.

Do C++0x initializer lists solve the problem?

Yes, they do. However your syntax is invalid, I think. You have to use braces directly to fire off list initialization

struct A {   int foo[3];   A():foo{1, 2, 3} { }   A():foo({1, 2, 3}) { } /* invalid */ }; 
like image 132
Johannes Schaub - litb Avatar answered Sep 23 '22 13:09

Johannes Schaub - litb


C++98 doesn't provide a direct syntax for anything but zeroing (or for non-POD elements, value-initializing) the array. For that you just write C(): arr() {}.

I thing Roger Pate is wrong about the alleged limitations of C++0x aggregate initialization, but I'm too lazy to look it up or check it out, and it doesn't matter, does it? EDIT: Roger was talking about "C++03", I misread it as "C++0x". Sorry, Roger. ☺

A C++98 workaround for your current code is to wrap the array in a struct and initialize it from a static constant of that type. The data has to reside somewhere anyway. Off the cuff it can look like this:

class C  { public:     C() : arr( arrData ) {}  private:      struct Arr{ int elem[3]; };      Arr arr;      static Arr const arrData; };  C::Arr const C::arrData = {{1, 2, 3}}; 
like image 41
Cheers and hth. - Alf Avatar answered Sep 24 '22 13:09

Cheers and hth. - Alf