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.

5
  • Are you talking about arrays that are data members of an object? Or are you talking about arrays of objects? Commented Apr 16, 2011 at 20:55
  • I'm talking about arrays that are data members of an object. Commented Apr 16, 2011 at 20:57
  • possible duplicate of C++ Initializing Non-Static Member Array Commented Apr 16, 2011 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. Commented Apr 16, 2011 at 21:14
  • Your question is limited to non-static members if you're limiting the question to object creation. Commented Apr 16, 2011 at 21:55

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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: You can also use it with vectors and other standard library containers, in which case, you actually are implicitly sizing it.
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: are you implying you were using std::memset() instead of std::fill_n()?
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.
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];
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.