Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: constructor initializer for arrays

I'm having a brain cramp... how do I initialize an array of objects properly in C++?

non-array example:

struct Foo { Foo(int x) { /* ... */  } };

struct Bar { 
     Foo foo;

     Bar() : foo(4) {}
};

array example:

struct Foo { Foo(int x) { /* ... */  } };

struct Baz { 
     Foo foo[3];

     // ??? I know the following syntax is wrong, but what's correct?
     Baz() : foo[0](4), foo[1](5), foo[2](6) {}
};

edit: Wild & crazy workaround ideas are appreciated, but they won't help me in my case. I'm working on an embedded processor where std::vector and other STL constructs are not available, and the obvious workaround is to make a default constructor and have an explicit init() method that can be called after construction-time, so that I don't have to use initializers at all. (This is one of those cases where I've gotten spoiled by Java's final keyword + flexibility with constructors.)

like image 597
Jason S Avatar asked Mar 09 '10 14:03

Jason S


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.

Do arrays need to be initialized in C?

An array may be partially initialized, by providing fewer data items than the size of the array. The remaining array elements will be automatically initialized to zero. If an array is to be completely initialized, the dimension of the array is not required.

How do you initialized an array in C?

Initializer List: To initialize an array in C with the same value, the naive way is to provide an initializer list. We use this with small arrays. int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index.

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.


3 Answers

There is no way. You need a default constructor for array members and it will be called, afterwards, you can do any initialization you want in the constructor.

like image 119
AProgrammer Avatar answered Oct 12 '22 15:10

AProgrammer


Just to update this question for C++11, this is now both possible to do and very natural:

struct Foo { Foo(int x) { /* ... */  } };  struct Baz {       Foo foo[3];       Baz() : foo{{4}, {5}, {6}} { } }; 

Those braces can also be elided for an even more concise:

struct Baz {       Foo foo[3];       Baz() : foo{4, 5, 6} { } }; 

Which can easily be extended to multi-dimensional arrays too:

struct Baz {     Foo foo[3][2];      Baz() : foo{1, 2, 3, 4, 5, 6} { } }; 
like image 24
Barry Avatar answered Oct 12 '22 14:10

Barry


Right now, you can't use the initializer list for array members. You're stuck doing it the hard way.

class Baz {
    Foo foo[3];

    Baz() {
        foo[0] = Foo(4);
        foo[1] = Foo(5);
        foo[2] = Foo(6);
    }
};

In C++0x you can write:

class Baz {
    Foo foo[3];

    Baz() : foo({4, 5, 6}) {}
};
like image 28
Michael Kristofik Avatar answered Oct 12 '22 16:10

Michael Kristofik