![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
Next: Limits
Up: More C++
Previous: String streams
Casts
C++ is strict about converting from one type to another - much stricter
that C is. You can use C-style casting but it's not recommended - instances
are hard to find in the code, and it's less type-safe.
Try to use the least dangerous (most specific) casting alternative
from those below
- const_cast
- - used to remove the const qualifier.
- dynamic_cast
- - does careful, type-sensitive casting at run time.
Can't deal with void*.
- reinterpret_cast
- - produces something that has the same
bit pattern as the original.
- static_cast
- - doesn't examine the object it casts from at
run time. This is fast if it's safe - and the compiler should
tell you if it isn't safe. Can
deal with void*.
static_cast can be used to reverse any of
the implicit conversions (like int to float).
To convert from a const void * to a File_entry**
(a common kind of thing to do in C) I had to do
File_entry**fe2= reinterpret_cast<File_entry**>(const_cast<void*>(entry2));
Next: Limits
Up: More C++
Previous: String streams
Tim Love
2001-07-05