How do I declare an array of objects whose class has no default constructor?

后端 未结 13 1092
时光取名叫无心
时光取名叫无心 2020-11-30 06:25

If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class



        
13条回答
  •  一向
    一向 (楼主)
    2020-11-30 06:39

    You have to use the aggregate initializer, with 10000 of inidividual initializers between the {}

    Foo array[10000] = { 1, 2, 3, ..., 10000 };
    

    Of course, specifying 10000 initializers is something from the realm of impossible, but you asked for it yourself. You wanted to declare an array of 10000 objects with no default constructor.

提交回复
热议问题