brekehan said:
I've always been a little sketchy on the differences between static,
dynamic, and reinterpret casting.
static_cast
static_cast is used to perform a cast between related types. Any of the
basic types (char, double, int, etc) can be converted using this cast.
It can also be used to up-cast or downcast classes in the same
inheritance tree (however, review dynamic_cast for reasons why you
might not want to do this). It correctly performs any necessary pointer
movement in order to point at the correct location within the object
for that static type. Consider that static almost always means
something performed at compile time that results in a hard coded
manipulation of the object into a new type.
dynamic_cast
dynamic_cast will perform a run-time cast of a polymorphic type. This
means that it is a safer but slower cast to objects sharing an
inheritance tree. This is usually used for a down cast that checks for
the validity of the cast before continuing. When a dynamic cast is
performed the system checks whether the object of cast is actually of
the type being cast to. Review the following:
class A {}; class B : public A {}; class C : public A {};
....
A * b = new B; A * c = new C;
....
C * bad = static_cast<C>(b); // also the result of a c style cast.
In this case the compiler happily performs the necessary conversion
between the related types B* and C* but the cast is obviously not
valid. Later something could blow up because B and C don't have the
same alignment properties. See the following:
C * bad = dynamic_cast<C>(b); If (bad)...ok.
In this case a dynamic, or run-time cast was used. The result of this
cast is a null pointer because b is not a C* and the cast is not valid.
This can now be checked and validated so that the program doesn't
just crash because someone didn't account for some situation when a
B* could be passed into a function that did this cast.
When this cast is performed on a reference it results in a bad_cast
exception instead of a null pointer.
const_cast
This is rarely necessary. If you need a const cast you should review
why and take the problem to the program manager for review. Do not cast
away constness without this review process.
Const cast is used to cast away constness or volatility. We do not use
volatility currently in pipe-flo so I will focus on constness. Const
cast should NEVER be used unless you are casting TO const. The
following is an example:
const char * x = "hello"; char * y = const_cast<char*>(x);
The goal of the above code is probably to change the text pointed to by
x. This results in undefined behavior and must not be done.
Sometimes one might be tempted to cast away constness in order to call
a non-const function that you are sure won't change anything. The
correct way to fix this problem is to make the function const. There
are a few cases when it is appropriate to change values internal to a
class in a const function (this also must be reviewed by the program
manager) and you can do this by making that variable "mutable".
One situation that const_cast is allowed without review is casting into
const:
class Obj {
...
void f() const { do stuff without changing anything}; void f() { do
something that changes stuff; const_cast<const Obj*>(this)->f(); } };
That uses the const version of f() instead of rewriting the same code.
This is of questionable design but not invalid so is allowed.
reinterpret_cast
This cast is used to cast between totally unrelated types. For
instance, you may have a C function that accepts char* as input and you
want to pass in an array of integers instead (often seen in binary file
output). This would be done as follows:
void cf(const char * input, size_t size);
.... int x[] = {5, 11, 23 }; cf(reinterpret_cast<char*>(x), sizeof(int)
* 3);
Notice that a const cast is not necessary because you can always add
const without a cast.
The above code formally results in undefined behavior but is necessary
when dealing with C code that doesn't have the strong typing and
templates available in C++.
You can always perform a cast from one type to another and back
(assuming that the size of the destination type is large enough to hold
the entire value of the source) and this will result in defined
behavior. For instance, passing data into a C callback (any win32 dlg
proc) you will often want to cast some pointer to the LPARAM type:
reinterpret_cast<LPARAM>(an_integer)
Later you can cast back:
reinterpret_cast<int>(lParam);
This is valid and defined code since the LPARAM type is large enough to
store any int value.