Some say the use of dynamic_cast often means bad design and dynamic_cast can be replaced by virtual functions
dynamic_cast
consi
In theory, down-casting should never be necessary. Instead you should adapt the base class to include the necessary virtual
method.
In practice, you encounter things such as 3rd party libraries. In this case, modifying the base class is not an option and thus you may be forced into using dynamic_cast
...
Back to your example:
class Animal {
public:
// starts moving toward `p`,
// throws a `Unreachable` exception if `p` cannot be reached at the moment.
virtual void moveToward(Point const& p) = 0;
}; // class Animal
And then:
bool move(Animal& animal, Point const& p) {
try {
animal.moveToward(p);
return true;
} catch (Unreachable const& e) {
LOG(animal.id() << " cannot reach " << p << ": " << e.what());
}
return false;
} // move