C ++ Array of 120 objects with constructor + parameters, header- + sourcefile, without pointers!

file.h:

extern objekt squares[120];

file.cpp:

objekt squares[120]= {objekt(objekt_size ,objekt_size ,-111,0)};

How can I initialize all objects at once, all with the same parameters?

+5
source share
2 answers

Do not use a raw array (because all elements will be initialized using the default constructor). Use for example. a std::vector:

std::vector<objekt> squares(120, objekt(objekt_size ,objekt_size ,-111,0));
+9
source

You can also use preprocessor to repeat the same code 120 times.

#include <boost/preprocessor/repetition/enum.hpp>

#define TO_BE_ENUMERATED(z, n, text) text

objekt squares[120] = {
    BOOST_PP_ENUM(120, TO_BE_ENUMERATED, objekt(objekt_size ,objekt_size ,-111,0))
};
#undef TO_BE_ENUMERATED
+1
source

All Articles