Question

Despite the fact that dynamic_cast returns a 0 if the pointer that is being handled is of an incompatible type, why would you avoid using dynamic_cast?

Was it helpful?

Solution

It takes non-zero runtime. That is about it. C-casts and their c++ counter parts like: reinterpret or static are 0-overhead because they are performed during compilation.

Well for some, important part might be that they do need RTTI, which also introduces some overhead, for example to the code size, because compiler has to include type information into binary, which is not normally done. One should take note that this might be non-standard option in the compilers.

Also relevant note from wiki: "In the original C++ design, Bjarne Stroustrup did not include run-time type information, because he thought this mechanism was frequently misused."

EDIT: Following on the quote and the comments. I am not sure if this is really a drawback, I would like to point out, that when you use it, you should think if you really do need it.

Some just dislike it, some do misuse it.

OTHER TIPS

Run-time overhead:

  • More memory is needed to store RTTI (see link).
  • Types must be checked at runtime.

Design Issues:

  • Types involved must be polymorphic.
  • Is often a sign of something else wrong in your code; why do you need to check?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top