dotnetchic wrote:
Can someone explain to me the difference between static_cast<T>(v) and
(what's the term for it?) old-style C cast?
UINT value1 = 0;
int value2 = (int)value1;
int value2 = static_cast<int>(value1);
static_cast is more explicit and more restrictive - you know on sight that a
static_cast does not:
- Cast away constness
- Cast between unrelated pointer types
- Reinterpret the bits of one type as another type
The C-style cast, however, is a chameleon - depending on the context, the
C-style (or function-style) cast is equivalent to either static_cast,
reinterpret_cast, or one of those two followed by const_cast.
So, in your example, there's no difference at all - the C-style cast in that
context is exactly the same as a static_cast. Use of the C++ casts is
prefereable because of the improved readability of the code, but there's no
functional (i.e. runtime or code generation) difference.
-cd