有人说the use of dynamic_cast often means bad design and dynamic_cast can be replaced by virtual functions
- 为什么使用被
dynamic_cast
认为是糟糕的设计? 假设我有 I function name
func(Animal* animal, int animalType)
, func 中的实现如下: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. */ }
这种情况是否正确使用dynamic_cast
?