In my game Bitfighter, I have a class called AbstractTeam that has two subclasses: Team and EditorTeam. They share many methods, but Team tracks spawn points (implementing addSpawn() and getSpawns() methods), whereas EditorTeam does not care at all about spawn points.
There are two designs I can see for implementing this:
-
I could implement addSpawn() and getSpawns() in Team but not in EditorTeam, then when I have an AbstractTeam, I could cast it to Team before accessing the methods.
-
I could implement addSpawn() and getSpawns() in AbstractTeam, making them do nothing, then override those in Team. That would eliminate the need for a cast, but would suggest that EditorTeam somehow cared about spawns, because it would now have the two (dummy) methods.
So my question is which is better?
The code is in C++, and I can provide some samples if the above is not clear.
Use number 1. That’s what
dynamic_castis for. You definitely should not define members in your base class that all derived classes will not implement.