The four ways to cast.
- This is covered in chapter 11 (P 357)
- And a reference
- The C style cast
- The first cast was
(type) expression
- This was hard to work with.
- The C++ style cast
- First c++ type of cast
-
type (expression)
- This was better but always confusing with multi word types.
-
static_cast
-
static_cast<type>(expression)
- There are a set of rules here
- But this is mostly for when a known conversion exists
- That is "safe"
- And does not rely on a vtable
- This is your stand in replacement for the c++ cast.
-
const_cast
- This trows away constant-ness.
- This should be used with extreme caution.
-
dynamic_cast
- For runtime casting.
- Looks at the vtable and see if the cast is possible.
- If so, do it and return the new pointer.
- Of not, return nullptr.
- This is perfect for what we are doing.
-
reinterpret_cast
- Do stuff you really shouldn't do.
- Including casts that are not allowed in c++.
- cast the bits of a float to the bits of an int.
- You are generally warned against doing this.