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

后端 未结 13 1112
时光取名叫无心
时光取名叫无心 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:54

    For an array you would have to provide an initializer for each element of the array at the point where you define the array.

    For a vector you can provide an instance to copy for each member of the vector.

    e.g.

    std::vector thousand_foos(1000, Foo(42));
    

提交回复
热议问题