1

I'm not sure how to use array variables with objects. How do you initialize an array when an object is created. An array that is a data member of an object.

I'm hoping to use an initialization list.

Justin Stryker
  • 373
  • 4
  • 13
  • Are you talking about arrays that are data members of an object? Or are you talking about arrays of objects? – In silico Apr 16 '11 at 20:55
  • I'm talking about arrays that are data members of an object. – Justin Stryker Apr 16 '11 at 20:57
  • possible duplicate of [C++ Initializing Non-Static Member Array](http://stackoverflow.com/questions/5643923/c-initializing-non-static-member-array) – ildjarn Apr 16 '11 at 21:05
  • @ildjarn It might be, but my question isn't limited to non-static Member arrays. I'm going to try out the solutions that are there. – Justin Stryker Apr 16 '11 at 21:14
  • Your question *is* limited to non-static members if you're limiting the question to object creation. – ildjarn Apr 16 '11 at 21:55

2 Answers2

4

If your compiler supports it, you can do it like this:

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

Soon enough, that will be standard. GCC supports it now, I'm not sure what other compilers do.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • Awesome, didn't know this worked, though it is super logical (practically identical to how you'd initialize a non-classmember array). Goodbye memset. Shame you can't use an implicit array length that way, but it's still cool. – Damon Apr 16 '11 at 22:43
  • @Damon: You can also use it with vectors and other standard library containers, in which case, you actually are implicitly sizing it. – Benjamin Lindley Apr 16 '11 at 23:01
  • About vectors I knew, just that arrays work std::initializer_list style was totally new to me. Which is great news, because I'm using not few arrays :-) I guess the reason why it won't implicitly size arrays is because empty-bracket arrays are an idiom of their own (like [0] arrays) and also because you could in theory have several constructors with different amounts of initializers, so the compiler might find picking the "correct" size ambiguous (though of course just picking the largest size used would be correct). – Damon Apr 16 '11 at 23:07
  • @Damon: are you implying you were using `std::memset()` instead of `std::fill_n()`? – Cubbi Apr 18 '11 at 19:08
  • Yes, that works nicely, and apart from the abysmal syntax and the obvious fact that it sets bytes, it's quite perfect. My compiler (gcc) entirely optimizes out memset initialization and instead stores different constants in the data segment. std::fill_n may be syntactically "cleaner" but boils down to a series of movdqa instructions. In the non-initialization case, they come down to more or less the same code. Now of course initializing an array with an initialization list like above is the best of two worlds. – Damon Apr 18 '11 at 20:50
2

An array member variable can only be default-initialized, you cannot provide explicit initialization values.

struct Foo {
  Foo() : bar() {}  // Default-initialize bar, for int this means initialize with 0
  int bar[10];
};

If you want anything else, you'll have to assign in the constructor body.

struct Foo {
  Foo() : bar() {
    bar[3] = 1;
  }  
  int bar[10];
};
Erik
  • 88,732
  • 13
  • 198
  • 189