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

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

    Actually, you can do it as long you use an initialization list, like

    Foo foos[4] = { Foo(0),Foo(1),Foo(2),Foo(3) };
    

    however with 10000 objects this is absolutely impractical. I'm not even sure if you were crazy enough to try if the compiler would accept an initialization list this big.

提交回复
热议问题