Avoiding dynamic_cast/RTTI

前端 未结 6 772
执笔经年
执笔经年 2020-12-16 23:13

I was recently working on a piece of C++ code for a side project (the cpp-markdown library, for the curious), and ran into a coding question that I\'d like some opinions on.

6条回答
  •  借酒劲吻你
    2020-12-17 00:04

    The true clean way to prevent dynamic_cast is to have pointers of the right type at the right place. Abstraction should take care of the rest.

    IMHO, the reason that dynamic_cast has this reputation is because its performance degrades a little bit every time you add another sub-type down in your class hierarchy. If you have 4-5 classes in the hierarchy, it's nothing to worry about.

提交回复
热议问题