Object *obj = new Object();
object_list.push_back(obj);
its very nice! just keep this in mind while desinging your next game... you dont have to manually track your items, you dont have to care what index that are at, hell, you dont even use index's anymore unless you work with a vector. the best part is, you dont have to know where your object is. all you have to do is register your object with a "master list", then the master list will update your object. for example, using the other ex., just do this in your main loop each frame"
for(int i = 0; < object_list.size(); i++)
object_list.Update();
this will call Update on all your objects! since. what i do is, make Object a fully abstract base class (is this what its called?), where it doesnt do anything, it simply has a virtual void Update() as a member. then, all your objects which need to Update() each frame, you inherit from the Object class. then you can stuff all your objects into a single container (or a vector of containers) and manage all your game objects from one list! the best part is, you dont even have to manage them yourself. simply register them with the list via push_back(), and voila, you have a self-managing object system.
since all your objects will Update() differently, just over-ride the Update() function in all your classes which inherit from Object (or whatever you name the class...). IMO this is the great advantage of C++ and OOP, inheritense, polymorphism, and cotainers... mmmmmmm. if you dont know what Polymorphism is yet, its not very complicated. polymorphism is simply grouping different classes togeather. so you can inherit Enemy from the Object class, and inherit Player from the Object class, and inherit Particle, or Bomb, or Map, or anything you want from the Object class... then you can group your player,enemies,map, bombs, all togeaher, even though they arent the same data type! to do this, you have to use pointers, since you cant directly goup the objects togeather, but you CAN diectly group there pointers togeather , which is just as good.
for managing your objects , since they will be allocated dynamically (a must since you need to use pointers for polymorphism), you could make Update() return bool, intead of void. then, when you want an object to be removed from the world, simpy return false from its Update(). then when you call Update, anyone returning false will be removed from the world. it could be when an enemy dies, or whenever you want an object to be removed. heres an example
inside Object.cpp
class Object{ public: virtual bool Update() = 0; private: static std::list<Object*> world_objects;};
inside Enemy.h / .cpp
class Enemy: public Object{ public: Enemy(int xpos,int ypos); virtual bool Update(); void Do_Schtuff(); private: bool im_dead;};bool Enemy::Update(){ //do anything an enemy does, do AI, move around, collision detection, WHATEVER! //at the end, we check if we died. if so, return false and remove us from the world - else return true and keep going if(im_dead) return false; else return true;}
now inside your main game loop.....
while(!game_over){//somewhere, whenever you want to add an enemy, you just do //create an enemy starting at (35,72) Enemy *new_enemy = new Enemy(35,72); //register this enemy with the world Object::world_objects.push_back(new_enemy); //now, each frame, you update your world!! for(it = Object::world_objects.begin(); it != Objects::world_objects.end()} { //update the object, and if he returned false, REMOVE HIM! if(!(*it)->Update()) { //delete the memory first delete *it; //now remove the object from the world Object::world_objects.remove(*it); } else it++; }}
do you see whats going on here? anything which needs to be Updated() each frame, inherits from Object. then you have a list of pointers to Objects, ie list<Object*> world_objects. this is your master list of objects! when you want a new being in the world, you register the object with the world, and the world will update it and manage it for you. it will delete the memory for you so no worries about memory leaks.... there might be some syntatical errors, im at work now so i dont have my code in front of me, but this is the gist of it! this is the ideal thing for a std::list, but dont be scared, they work almost exactly the same as a vector does, except you cannot index a list via [x], like with a vector, which is why i used iterators. dont be scared about the iterators either, you will learn them when you learn about the other containers, they are just how you loop through the elements of a container, since you can index a list like you cant a vector (since they arent sequential in memory like an array). like i said, dont be scared about the other types of containers - they are all very much like a vector - and a vector is very much like an array. but each container has its own purpose, and a list is best for this. good luck!!