Objects with arguments and array

前端 未结 4 533
轮回少年
轮回少年 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:10

    When constructing an array of objects in C++ only the default constructor can be used unless you're using the explicit Array initialization syntax:

    Object myObject[5] = { Object( x, y ),
                           Object( x, y ),
                           Object( x, y ), 
                           Object( x, y ), 
                           Object( x, y ) }
    

    Here's some good information from the C++ FAQ about this:

    http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.5

提交回复
热议问题