You can implicitly convert between numerical types, even when that loses precision:
char c = i;
However, you might like to enable compiler warnings to avoid potentially lossy conversions like this. If you do, then use static_cast
for the conversion.
Of the other casts:
dynamic_cast
only works for pointers or references to polymorphic class types;
const_cast
can't change types, only const
or volatile
qualifiers;
reinterpret_cast
is for special circumstances, converting between pointers or references and completely unrelated types. Specifically, it won't do numeric conversions.
- C-style and function-style casts do whatever combination of
static_cast
, const_cast
and reinterpret_cast
is needed to get the job done.