Possible Duplicate:
Intitialzing an array in a C++ class and modifiable lvalue problem
As seen in this question, it's possible to give a ctor to a struct to make it members get default values. How would you proceed to give a default value to every element of an array inside a struct.
struct foo
{
int array[ 10 ];
int simpleInt;
foo() : simpleInt(0) {}; // only initialize the int...
}
Is there some way to make this in one line similar to how you would do to initialize an int?
If you just want to default-initialize the array (setting built-in types to 0), you can do it like this:
Thew new C++ standard has a way to do this:
test: https://ideone.com/enBUu
If your compiler does not support this syntax yet, you can always assign to each element of the array:
EDIT: one-liner solutions in pre-2011 C++ require different container types, such as C++ vector (which is preferred anyway) or boost array, which can be boost.assign'ed
Changing the array to a std::vector will allow you to do simple initialization and you'll gain the other benefits of using a vector.
or use
std::generate(begin, end, generator);
where the generator is up to you.