samedi 9 juillet 2016

creating an array of object pointers C++


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?


Aucun commentaire:

Enregistrer un commentaire