Quote:Original post by blaze02
The correct way would be to have an enumerated type or some type of flag in the base class.
That's unsafe: the flag or enumerated type could be set incorrectly because of human error, while dynamic_cast works on any class that uses inheritance (and thus, has a virtual destructor). And it's also more difficult to manage an inheritance lattice with only enumerated types, while dynamic_cast will handle type lattices without a sweat.
There are other ways, for instance using virtual functions as a semantic replacement for dynamic_cast, which need about as much work as an enumeration-based system, but are type-safe and can handle lattices. Still, they rely on human intervention and as such are prone to semantic bugs.
Of course, the best way (and the one that is the most often available and thrust forward in OOP) would be to use virtual functions to represent operations, and rework the design so the inheritance tree fits together better.