Dlaczego dynamic_cast jest zły, czy nie? Czy w tym przypadku powinienem użyć dynamic_cast?
Niektórzy mówiąthe use of dynamic_cast often means bad design and dynamic_cast can be replaced by virtual functions
dynamic_cast
za zły projekt?Załóżmy, że mam nazwę funkcjifunc(Animal* animal, int animalType)
, implementacja w func wygląda następująco:
bool func(Animal* animal, int animalType)
{
...
/* Animal is the base class of Bear, Panda, Fish ....
dynamic_cast animal to real animals(Bear, Panda, Fish...)
according to animalType. Do some processing with this specific
type of animal, using its additional information beyond base
class Animal. */
}
Czy to przypadek jest właściwym użyciemdynamic_cast
?