Original Post
I am developing a 2D game based on Red Alert game.
I have a tank class and an infantry class, among others.
I want to be able to pass into a subroutine a pointer to either
a instance of the tank class or an instance of the infantry class, and possibly a flag telling which type. How can the subroutine determine which type of class the pointer points to?
I would like to avoid using a case/if statement which duplicates large portions of the subroutine since this is not much different than just having separate subroutines.
Something like this?? -
void func1(CGenericUnit* pGenericUnit, int type) {
if (type == TANK) {
CTankUnit* pUnit;
pUnit = (CGroundUnit*)pGenericUnit;
}
else if (type == INFANTRY) {
CInfantryUnit* pUnit;
pUnit = (CInfantryUnit*)pGenericUnit;
}
// now use pUnit freely except locality is broken
pUnit->setLocation(location);
// etc
}
thanks in advance for any help you are able to provide,
steve coward