The new C ++ standard has this way:
struct foo { int array[ 10 ]; int simpleInt; foo() : array{1,2,3,4,5,6,7,8,9,10}, simpleInt(0) {}; };
test: https://ideone.com/enBUu
If your compiler does not yet support this syntax, you can always assign an array to each element:
struct foo { int array[ 10 ]; int simpleInt; foo() : simpleInt(0) { for(int i=0; i<10; ++i) array[i] = i; } };
EDIT: single-line solutions in pre-2011 C ++ require different types of containers, such as a C ++ vector (which is preferable anyway) or a boost array, which can be boost.assign 'ed
#include <boost/assign/list_of.hpp> #include <boost/array.hpp> struct foo { boost::array<int, 10> array; int simpleInt; foo() : array(boost::assign::list_of(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)), simpleInt(0) {}; };
Cubbi source share