C++, array of objects without

后端 未结 9 1746
渐次进展
渐次进展 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:08

    Use an array of pointers to Object2:

    std::tr1::shared_ptr* newArray = new shared_ptr[numberOfObjects];
    for(int i = 0; i < numberOfObjects; i++)
    {
        newArray[i] = shared_ptr(new Object2(params));
    }
    

    Or, alternatively, without the use of shared_ptr:

    Object2** newArray = new Object2*[numberOfObjects];
    for(int i = 0; i < numberOfObjects; i++)
    {
        newArray[i] = new Object2(params);
    }
    

提交回复
热议问题