Sign in to follow this  
TheMoebius

the overloaded function that just wouldn't

Recommended Posts

TheMoebius    112
I have a class called Ship and a child class called ShipAI. The update() function in ShipAI is supposed to overload the update() function is ship, but its not. Here's a bit of the code from the header file.
class Ship {
(snip)
public:
	
	Ship();
	
	//this is called every frame by WorldMgr
	void update(Real timeSinceLastFrame);
(snip)
};




class ShipAI: public Ship {
(snip)
public:
	ShipAI();
	void update(Real timeSinceLastFrame);
};

I have a std::vector of pointers to Ship and each frame is cycles through that list and calls the update() function. Here's the code:
vector<Ship*>::iterator i;
for (i=mShips.begin(); i!=mShips.end(); i++){
	//cout << "updating ship" << (*i)->getShipID() << endl;
	(*i)->update(evt.timeSinceLastFrame);
}
The list includes Ship's and ShipAI's. I would have thought that when it came across a ShipAI class it would call ShipAI::update() instead of Ship::update(), but it doesn't. I have cout traces in the ShipAI constructor and the Ship constructor and in ShipAI::update(). On world creation I manually spawn one Ship class and one ShipAI class. I see the output of Ship twice and ShipAI once so I know it's being created properly. Am I an idiot or whats going on? Thx.

Share this post


Link to post
Share on other sites
Fred304    382
Quote:
Original post by TheMoebius
The update() function in ShipAI is supposed to overload the update() function is ship

You mean "overwrite", right?

Quote:

void update(Real timeSinceLastFrame);

Make the method virtual, then it should work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this