- C-style casts
Is for widening numeric conversions (e.g.,
float), converting between pointer types and
Can also be used to call explicit (or implicit) conversion functions.
Can cast around the hierarchy but it isn’t checked.
Add or remove
const to a variable.
Not so important because of
Can be used to call
const-overloaded member functions.
Can also add or remove
For casting around hierarchies.
nullptr on failure for a pointer, or throw
std::bad_cast for references.
Can cast sideways and up other hierarchies
Doesn’t work if you have a diamond and haven’t used virtual inheritance
Only works for public inheritance
Will convert anything into anything.
Use to cast a pointer to or from an opaque type, or to cast a blob of memory to a POD type
Is reversible unless you cast to a type with less storage space
This is the same thing as a "function-style cast"
Perform the new casts in order:
dynamic_cast is not in this list – you can cast around the hierarchy with C-style casts, but it isn’t checked