Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

file.h:

extern objekt squares[120];

file.cpp:

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

How can I init all objects at one time, all with the same parameters?

like image 596
Vincent Avatar asked Feb 12 '11 21:02

Vincent


2 Answers

Don't use a raw array (because all the elements will be initialised via the default constructor). Use e.g. a std::vector:

std::vector<objekt> squares(120, objekt(objekt_size ,objekt_size ,-111,0));
like image 159
Oliver Charlesworth Avatar answered Oct 30 '22 20:10

Oliver Charlesworth


You can also use the 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
like image 22
UncleBens Avatar answered Oct 30 '22 19:10

UncleBens