Almost every C++ resource I\'ve seen that discusses this kind of thing tells me that I should prefer polymorphic approaches to using RTTI (run-time type identification). In
It looks kind of neat in a small example, but in real life you will soon end up with a long set of types that can poke each other, some of them perhaps only in one direction.
What about dark_orange_node
, or black_and_orange_striped_node
, or dotted_node
? Can it have dots of different colors? What if most dots are orange, can it be poked then?
And each time you have to add a new rule, you will have to revisit all the poke_adjacent
functions and add more if-statements.
As always, it is hard to create generic examples, I'll give you that.
But if I were to do this specific example, I would add a poke()
member to all the classes and let some of them ignore the call (void poke() {}
) if they are not interested.
Surely that would be even less expensive than comparing the typeid
s.