Original Post
I'm writing a system for handling ships in a new game of mine, and I've run into a bit of a problem -- mostly with my inexperience with using polymorphism. I have class Ship which inherits from a class Rotatable. Ship is meant to be a base class for more specialized ship classes (IE: Frigate, Corvette, etc). Whenever a new ship is spawned, it is placed into a list of ship objects (list allShips) and I use an iterator (list::iterator currentShip) to deal with each ship one by one. I then call class functions as needed (in the form currentShip->render() etc...). The problem is that when called, the function as defined in Ship is always called, not as defined in the subclass (Frigate, etc). Anyone know what I'm doing wrong? A bit of relevant code is below: Here is the list and ship handler function And here I create a couple of ship objects (of class TestShip) for testing purposes.
list<Ship> Allships;
list<Ship>::iterator currentship;
void HandleShips()
{
for(currentship = Allships.begin(); currentship != Allships.end(); currentship++)
{
currentship->RotateX(0.005);
currentship->Render();
}
}
Allships.push_back(TestShip(100, 100, 0.0, 0.0, 0.0));
Allships.push_back(TestShip(100, 100, 0.0, 0.1, 0.0));