I know that dynamic_cast
performs a runtime check and therefore is considered safer (can return null pointer on failure) but slower then static_cast
. But how large is the difference in overhead between the two?
Should I really consider using static_cast
in loops for performance reasons in regular large projects? Or is the difference minor and only relevant for special real-time programs?
Did you profile it?
The rule is:
static_cast
when you know that the target type is valid.dynamic_cast
when you're not sure, and you need the program to look up the object's runtime type for you.It's as simple as that. All other considerations are secondary.