Test a Class type & Heritage

Hi everyone !

I encountered a problem while I was coding a game in C++ about Class types and Heritage.

My raw question is the following : Is there a way to know the under class type of a mother class passed in parameter ?

I apologize for my expression style i promise I will do my best to be understood at best.

I have a Class named "SpaceShip" and two under classes that heritate from it named "EnemySpaceShip" and "PlayerSpaceShip".
Those last ones can fire missiles (that is another class) thanks to a function that take in parameter a SpaceShip.
Then, in my function, i would test :

void fireMissile(SpaceShip *mySpaceShipThatShoot){
If ( mySpaceShipThatShoot belong to "EnemySpaceShip")
create new missile for IA;

Else If (PlayerSpaceShip belong to PlayerSpaceShip)
create new missile for Player;

}

I know a lot of ways to solve it, i actually do it with a boolean gave in parameter. Have another solution to this problem do no interests me at all.

I would know if a solution in c++ or using "SFML" library exists to test the under-class of the object took in parameter.

Hope I wasn't too much vague in my explanations, thank you for reading ;) !
Faya.
Do you really need player spaceship and enemy spaceship be two different classes? What the difference between them? Looks like you using inheritance where it isn't needed. Maybe it is better to add a field "owner" to SpaceShip class or use classes Player and Enemy who has-a SpaceShip?
Topic archived. No new replies allowed.