16

After reading How to initialize an array in C, in particular:

Don't overlook the obvious solution, though:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

I tried something like this:

#include <iostream>

class Something {
private:

int myArray[10];

public:

Something() {
    myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}

int ShowThingy(int what) {
    return myArray[what];
}

~Something() {}
};

int main () {
   Something Thing;
    std::cerr << Thing.ShowThingy(3);
}

And I get:

..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:10:48: error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment

The obvious in this case is not so obvious. I really would like the initiation of my array to be more dynamic as well.

I tired:

private:
    int * myArray;

public:
    Something() {
            myArray = new int [10];
            myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}

This looked funky to me to, and so to the compiler:

..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:11:44: error: cannot convert '<brace-enclosed initializer list>' to 'int*' in assignment

This also did not work:

private:
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

with:

 ..\src\Something.cpp:6:20: error: a brace-enclosed initializer is not allowed here before '{' token
 ..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers
 ..\src\Something.cpp:6:51: error: 'constexpr' needed for in-class initialization of static data member 'myArray' of non-integral type

I have been doing really good and learning what does not work, but not so good learning what does work.

So, how do I used initialization lists {value, value, value} for an array inside a class?

I have been trying to figure out how to do this for some time now and am very stuck, I have a number of these kinds of lists I need to make for my app.

Community
  • 1
  • 1
Quade2002
  • 615
  • 2
  • 7
  • 23
  • 1
    One of the stupid things about raw arrays in C++ is that they're not directly assignable. (I.e., the following isn't allowed. `int a[10], b[10]; a = b;`) – bames53 May 22 '12 at 01:45
  • 1
    Since C++11 is being mentioned in this post, I'll point out std::array: `std::array a = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; std::array b = a; std::array c; c.fill(5);` – Oliver Seiler May 22 '12 at 03:43

3 Answers3

27

You need to initialize the array in the constructor initialization list

#include <iostream>

class Something {
private:

int myArray[10];

public:

Something()
: myArray { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }
{
}

int ShowThingy(int what) {
    return myArray[what];
}

~Something() {}
};

int main () {
   Something Thing;
    std::cerr << Thing.ShowThingy(3);
}

..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers

C++11 also adds supports for inline initialization of non-static member variables, but as the above error message states, your compiler has not implemented this yet.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • You can do that? Very cool, in which standard was that added? – derekerdmann May 22 '12 at 01:37
  • 2
    @derekerdmann [Initializer lists](http://en.cppreference.com/w/cpp/utility/initializer_list) were added in C++11 – Praetorian May 22 '12 at 01:39
  • @derekerdmann gcc 4.5.1 [supports](http://ideone.com/VI0kA) it. I know VC++ doesn't, even in the upcoming VC11 version, not sure of about compilers. – Praetorian May 22 '12 at 01:42
  • So does this means before C++11 `myArray` can only be initialized by a loop or memset function? – shengy May 22 '12 at 01:43
  • 1
    @shengy - I believe so. At the very least, you can make a static constant that you can copy when you create a new instance. – derekerdmann May 22 '12 at 01:44
  • 1
    @Prætorian, IIRC from a comment on one of my other answers, using both parentheses and an initializer list is incorrect, albeit compiling fine on GCC. – chris May 22 '12 at 01:44
  • @derekerdmann I believe latest versions of clang, gcc and intel c++ have it. – bames53 May 22 '12 at 01:45
  • @Prætorian, I found the question. Here it is: http://stackoverflow.com/questions/10372058/syntax-error-missing-before/. Using ({0}) is a warning in GCC 4.7.0. – chris May 22 '12 at 01:51
  • @chris You're right, gcc4.7 warns about that (`warning: list-initializer for non-class type must not be parenthesized [enabled by default]`); thanks for pointing it out. – Praetorian May 22 '12 at 01:56
1

Unless I'm mistaken, the initializer list is only allowed for when the variable is initialized during declaration - hence the name. You can't assign an initializer list to a variable, as you're trying to do in most of your examples.

In your last example, you're trying to add static initialization to a non-static member. If you want the array to be a static member of the class, you could try something like this:

class Derp {
private:
    static int myArray[10];
}

Derp::myArray[] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

If you want to add a class member, you could try making the static array const and copy it into the member array in the constructor.

derekerdmann
  • 17,696
  • 11
  • 76
  • 110
  • 2
    Technically you can assign an initializer list to a variable of type `std::initializer_list` in C++11. – chris May 22 '12 at 01:56
-1

It is possible to initialize array values of the array in size the main function like this:

Array *arr2 []= {{1, 3, 5, 6, 7},20,5};
user438383
  • 5,716
  • 8
  • 28
  • 43