creating an array of object pointers C++

给你一囗甜甜゛ 提交于 2019-12-03 06:01:09

问题


I want to create an array that holds pointers to many object, but I don't know in advance the number of objects I'll hold, which means that I need to dynamically allocate memory for the array. I have thought of the next code:

ants = new *Ant[num_ants];
for (i=1;i<num_ants+1;i++)
{
    ants[i-1] = new Ant();
}

where ants is defined as Ant **ants; and Ant is a class.

Will it work?


回答1:


Will it work?

Yes.

However, if possible, you should use a vector:

#include <vector>

std::vector<Ant*> ants;
for (int i = 0; i < num_ants; ++i) {
    ants.push_back(new Ant());
}

If you have to use a dynamically allocated array then I would prefer this syntax:

typedef Ant* AntPtr;
AntPtr * ants = new AntPtr[num_ants];
for (int i = 0; i < num_ants; ++i) {
    ants[i] = new Ant();
}

But forget all that. The code still isn't any good since it requires manual memory management. To fix that you could to change your code to:

std::vector<std::unique_ptr<Ant>> ants;
for (auto i = 0; i != num_ants; ++i) {
    ants.push_back(std::make_unique<Ant>());
}

And best of all would be simply this:

std::vector<Ant> ants(num_ants);



回答2:


std::vector<Ant> ants(num_ants);
ants.resize(new_num_ants);



回答3:


Do you really need to hold pointers to the items? If you can use objects by value, a far simpler approach is to use a vector: std::vector<Ant> ants(num_ants);. Then not only do you not have to write looping, but you don't have to worry about memory leaks from raw pointers and other object management items.

If you need object pointers to say satisfy an API you can still use vector for the outer container and allocate the objects manually.

struct CreateAnt
{
    Ant* operator()() const { return new Ant; }
};

std::vector<Ant*> ants(num_ants);  // Create vector with null pointers.
std::generate(ants.begin(), ants.end(), CreateAnt());



回答4:


std::vector<Ant*> ants( num_ants );
for ( int i = 0; i != num_ants; ++ i ) {
    ants[i] = new Ant;
}

Or if you don't know how many in advance:

std::vector<Ant*> ants;
while ( moreAntsNeeded() ) {
    ants.push_back( new Ant );
}

On the other hand, I think you need to ask yourself whether Ant is an entity type or a value. If it's a value, you'll probably want to skip the pointers and the dynamic allocation; if it's an entity type, you'll have to consider the lifetime of the object, and when and where it will be deleted.




回答5:


Yes that's the general idea. However, there are alternatives. Are you sure you need an array of pointers? An array of objects of class Ant may be sufficient. The you would only need to allocate the array:

Ant *ants = new Ant[num_ants];

In general, you should prefer using std::vector to using an array. A vector can grow as needed, and it will handle the memory management for you.

In the code you have posted, you would have to delete each element of ants in a loop, and then delete the array itself, delete [] ant. Keep in mind the difference between delete and delete [].

One more point, since array indices in C++ are 0-based, the following convention is used to iterate over the elements:

for (i=0; i<num_ants; i++)
{
    ants[i] = new Ant();
}

This makes code much more readable.



来源:https://stackoverflow.com/questions/5887615/creating-an-array-of-object-pointers-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!