开发者

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

开发者 https://www.devze.com 2023-02-10 16:56 出处:网络
file.h: extern objekt squares[120]; fi开发者_如何学Cle.cpp: objekt squares[120]= {objekt(objekt_size ,objekt_size ,-111,0)};

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
0

精彩评论

暂无评论...
验证码 换一张
取 消