C++ Instantiate class and add name to array/vector then cycle through update functions from the array

Posted by SD42 on Stack Overflow See other posts from Stack Overflow or by SD42
Published on 2011-01-08T06:09:28Z Indexed on 2011/01/08 6:53 UTC
Read the original article Hit count: 134

Filed under:

I have a bunch of classes in a basic (and badly coded!) game engine that are currently instantiated in the code and have to have their individual update functions called. What I want to do is be able to create an instance of a class, pass the name to an array, and then subsequently cycle through the array to call the update functions of each class. I'm unsure as to whether or not this is an impossible or spectacularly stupid way of trying to manage objects, so please tell me if it is.

So currently I might have manually instantiated Enemy class a couple of times:

Enemy enem1;
Enemy enem2;

I then have to update them manually in the game loop:

enem1.update();
enem2.update();

What method should I use to be able to spawn and destroy instances of the enemy class during gametime? Is it possible to populate an array with instantiated names and then do something like (and i'm aware this doesn't work);

array[x].update();

Then iterate through the names in the array?

Anything that even points me in the right direction would be greatly appreciated!

© Stack Overflow or respective owner

Related posts about c++