This should be an answer, but it may not be /the/ answer. What you're doing is awful. You will have something like:
void addObject(Object* ob) {
if(ob->isA()) { doA(ob); }
if(ob->isB()) { doB(ob); }
if(ob->isC()) { doC(ob); }
genericObjectStuff(ob);
}
This is an "antipattern" because it is bad, it means for each derived you have (in your case Tile
is CURRENTLY the only one) you'll have to come back to this function and add any special behaviour.
There are two fixes (that depend on what the objects in play are like)
1) Virtual functions:
void addObject(Object* ob) {
ob->doSpecificThingYouNeedToDo(this);
genericObjectStuff(ob);
}
Where doSpecificThingYouNeedToDo
will doA(this);
inside if it is an A, do B if it is a B.... thus you write what it does when it is added where you define the class! Much better!
2) You've abstracted away too much!
If I give you a DrivableThing
you know you can do whatever it means to be a DrivableThing
to it, you are not given that it is a Truck
say (which IS A DrivableThing
) so you cannot loadCargo
on the drivable thing because it might not be a truck!
So a function that loads cargo can only accept Truck
s, or some other type that is drivable and loadable. It'd be wrong to have a generic function that loaded trucks, but also put pizzas on the back of motorbikes (Delivery task also!)
This is the opposite of 1, rather than adding the functionality to the derived classes, you want different addObject
s that accept the specific object only. Because they require this information, so you currently have:
void loadUpCargo(DrivableThing* thing) {
if(thing->isPizzaBike()) { thing->addPizzas(whatever); }
if(thing->isTruck()) { thing->addCrates(whatever); }
thing->driveOff(); //we can always drive off DrivableThings
}
You can change this to:
void loadUpCargo(Truck* truck) {
truck->addCrates(whatever);
truck->driveOff();
}
void loadUpCargo(PizzaBike* bike) {
bike->addPizza(whatever);
bike->driveOff();
}
Much better!
The classic example of this is "Animal class has an Eat method" "Bananas are Eatable, a Monkey is an Animal" but having an Animal and an Eatable wont stop you feeding Grass to a Monkey because you've abstracted the information away!