"Scott Brady Drummonds" <sc**********************@intel.com> wrote in message news:<bu**********@news01.intel.com>...
Hi, everyone,
I've checked a couple of on-line resources and am unable to determine how
reinterpret_cast<> is different from static_cast<>. They both seem to
perform a compile-time casting of one type to another. However, I'm certain
that there is something else that is happening.
Can someone explain the difference or recommend an online site that can
explain it to me?
Thanks,
Scott
Interestingly enough I just went through the process of laying some
ground rules on the use of the C++ casts.
A synopsis of what I was able to deduce - of course the experts will
correct me if I'm mistaken
---------
reinterpret_cast is ONLY for pointers, of the same constness. You can
cast a char* to an unsigned char* or any other * with
reinterpret_cast, but you can't cast off constness with reinterpret
cast.
const_cast is to cast off constness. If you have a const char* and
ABSOLUTELY MUST get rid of its constness and KNOW FOR A FACT that that
operation is safe AND you can't just make a new copy of the buffer ...
then const_cast is your tool.
static_cast is for casting a base class to a derived class. No
runtime type checking is performed so this seems/is inherently
dangerous, but sometimes if you are stuck with a bad design ...
unavoidable. You can also cast an int to an enum using static cast,
as well as a few other things.
dynamic_cast is a "safe" way to cast a base class to a derived class,
but it requires you to include type information in your build. if at
runtime, if the cast can't be performed, then the cast returns 0,
otherwise the cast succeeds and you continue your program.
---------
Along the same lines I have a question.
Notwithstanding the C++ casts are limited in scope which in my view
implies (no proof though) that they are safer than the C variant,
whats the 'real' gain over the C cast? A question that seems to crop
up with no reasonable answer on my part. I suspect in the grand
scheme of things casting it's low level bit twiddling on both parts
(C/C++)
----------