Why is dynamic_cast evil or not ? Should I use dynamic_cast in this case?
Some say the use of dynamic_cast often means bad design and dynamic_cast
can be replaced by virtual functions
(1)why is the use of dynamic_cast bad design ?
(2)Suppose I have I function name func(Animal* animal, int animalType) ,
the implementation in func is like:
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.
}
Is this case a proper use of dynamic_cast ?
No comments:
Post a Comment