Objects with arguments and array

前端 未结 4 519
轮回少年
轮回少年 2021-01-06 06:36

Is there a way in C++ where an objects has argument added upon it, with an array such as:

int x = 1;
int y = 2;

Object myObject( x, y )[5]; // does not work         


        
4条回答
  •  既然无缘
    2021-01-06 07:16

    If you don't mind using a vector instead of an array:

    std::vector obj_vec(5, Object(x, y));
    
    
    

    Or if you really want an array and don't mind initializing it in 2 steps:

    Object obj_array[5];
    std::fill_n(obj_array, 5, Object(x, y));
    

    提交回复
    热议问题