Creating objects in a loop?

So, is this even possible? I sure can't think of a way that it would work. Basically, I'm creating a 2D tower defense game, and at the beginning of each wave, I need to create several objects of my enemy class. At first, I was just like "Oh well, I'll just make a loop and each iteration of the loop makes an object, blah blah." Then realized, this is not possible. So, I'm trying to think of a solution to this problem. Luckily, I'm still in planning phase so all suggestions are equal, as I have no code I would have to rewrite

EDIT: I may actually be wrong, and this sounds as if it'll work. At least in Java, which is what the project is going to be written in.

Now, to keep this a C++ topic, and due to curiosity, is it possible to do the same thing in C++?
Last edited on
Why should it not be possible in your eyes?
First thing I thought of was a vector.

1
2
3
4
5
6
7
vector<enemyClass> vectorOfEnemies;
for (int i=0; i<numberOfEnemies; ++i)
{
  enemyClass someNewEnemyObject;
  vectorOfEnemies.push_back(someNewEnemyObject);
  // a whole bunch of other stuff I expect
}
I remember being told to learn linked lists for a task similar to this, try looking that up
I thought it wouldnt be possible because I would have a series of objects with the same name.

Linked list you say? Not sure how that would apply, but I'll look it up
The container you use will depend mostly on how you intend to access them, use vector as Moschops suggested if you want random access, use a list if you want to insert at random positions or if you want the objects in a stack.
closed account (LhUM4iN6)
Creating objects in a loop is always valid. It is very common too.

"I thought it wouldnt be possible because I would have a series of objects with the same name. "
that won't be a problem because everytime it loops back, all objects within the loop scope will be destructed, so the object no longer exists.

you probably made the same error like Moschops code, someNewEnemyObject gets destructed everytime the loop iterates, so what it push_back in the vector no longer exists.
If you need to create objects inside loop and don't want it to get destructed, you have to allocate memory of the object and store its pointer address.

For example:
vector<enemyClass*> vectorOfEnemies;
for (int i=0; i<numberOfEnemies; ++i)
{
enemyClass* someNewEnemyObjectPointer = new someNewEnemyObject();
vectorOfEnemies.push_back(someNewEnemyObjectPointer);
// a whole bunch of other stuff I expect
}
// make sure you deallocate the memory when you are done with your vector
for (int i=0; i<vectorOfEnemies.size(); i++)
{
delete vectorOfEnemies[i];
}

note that the pointer object someNewEnemyObjectPointer gets destructed on every iteration, but it is merely a pointer that points to the actual content, and the content created using "new" stays. "new" allocates memory and remain in the program until it is manually dellocated
you probably made the same error like Moschops code, someNewEnemyObject gets destructed everytime the loop iterates, so what it push_back in the vector no longer exists.

That's nonsense. The vector stores a copy of the object. Moschops' code is perfectly valid and the correct answer to OP's question.
Topic archived. No new replies allowed.