Quote:Original post by BoReDoM_Inc
But I a still using an array of each type of object (inherited ship) and then using the vector to store information about that object. Is this right or is there some better way that will leave me open to have as many instances of an object that I want?
class IObject{public: virtual void DoSomething() = 0;};class CShip : public IObject{public: void DoSomething() { printf("CShip::DoSomething()\n"); }};class CMissile : public IObject{public: void DoSomething() { printf("CMissile::DoSomething()\n"); }};class CAsteroid : public IObject{public: void DoSomething() { printf("CAsteroid::DoSomething()\n"); }};void main(){ list<IObject *> gameObjects; //Contains ships, missiles and asteroids IObject * o = new CShip; gameObjects.push_back(o); o = new CAsteroid; gameObjects.push_back(o); o = new CMissile; gameObjects.push_back(o); for (list<IObject *>::iterator i = gameObjects.begin(); i != gameObjects.end(); ++i) (*i)->DoSomething();}
Output:
CShip::DoSomething()
CAsteroid::DoSomething()
CMissile::DoSomething()
If you have a FrameUpdate() function for each type of object, they'll all update correctly without you having to care what type they are. All you know is you have an IObject pointer, and calling FrameUpdate() will do exactly what it's supposed to do. You don't have to worry about the actual type of the object except when constructing it.