Say I have something like — this is just an example mind you.
class Car
{
void accelerate();
void stop();
}
class Person
{
void drive(Car car);
}
class Toyota : public Car
{
void accelerateUncontrollably();
}
class ToyotaDriver : public Person
{
void drive(Car car)
{
// How to accelerateUncontrollably without dynamic cast?
}
}
A couple things, Toyotas and ToyotaDriver go together, i.e. I can have a ToyotaFactory class which will return the driver and the car. So the pieces are interchangeable and used in different parts of the code but a Toyota and a ToyotaDriver go together.
My impression is that using a
dynamic_castis absolutely fine here. No need to avoid it.