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
?
2 Answers
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.

- 15,812
- 38
- 62
-
And it requires the class involved to be polymorphic. – Angew is no longer proud of SO May 02 '14 at 18:04
-
-
-
1but there are use-cases where the dynamic_cast can't be replaced by static_cast or reinterpret_cast. This may or may not be bad design, depending on your opinion (see Bjarnes citation from wiki above). – Tobias Langner May 02 '14 at 18:10
-
@TobiasLangner I, or he never said, it is bad. He didn't like it because he though people would misuse it. I would compare it to selling or not knives. They can be used for something there is no proper replacement or misused for hurting people or oneself =). I wouldn't ban them though. I wanted just to point out the opinions are quite split over this topic. – luk32 May 02 '14 at 18:17