c++ - c++03 Initializing a array of objects with multiple parameters -


this might simple question trying initialize array of objects using parameterized constructor. example:

class a{ public:     int b,c,d;     (int i, int j); };  void a::a(int i, int j){     d = rand()     b = 2*i;     c = 3*j; }  void main(){     a[50]; /*initialize 50 objects using constructor*/ } 

i have tried vector initialization mentioned in this link however, since there 2 parameters, not work.

also, mentioned in link, not possible , tedious manually enter 50 initialization values.

is there easier way. also, i,j values same objects (available through main()) d should random value , differs each object.

you can use std::generate

example:

a generator(){ return a(1,2); }  std::generate( a, + (sizeof(a) / sizeof(a[0])), generator ); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -