C++ Array of 120 objects with constructor + parameters, header- + sourcefile, no pointers please!
file.h:
extern objekt squares[120];
fi开发者_如何学Cle.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?
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));
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
精彩评论