C++, array of objects without

后端 未结 9 1752
渐次进展
渐次进展 2020-12-08 07:34

I want to create in C++ an array of Objects without using STL.

How can I do this?

How could I create array of Object2, which has no argumentless constructor

9条回答
  •  余生分开走
    2020-12-08 08:10

    The obvious question is why you don't want to use the STL.

    Assuming you have a reason, you would create an array of objects with something like Obj * op = new Obj[4];. Just remember to get rid of it with delete [] op;.

    You can't do that with an object with no constructor that doesn't take arguments. In that case, I think the best you could do is allocate some memory and use placement new. It isn't as straightforward as the other methods.

提交回复
热议问题