R
Ravi
What is the difference between static cast and reinterpret cast?
Ravi said:What is the difference between static cast and reinterpret cast?
With static_cast you may only cast within the same inheritance
hierarchy, i.e. it is more restrictive and thus somewhat safer.
dynamic_cast is similar, adding run-time checking and making the cast
really safe.
reinterpret_cast allows any cast.
Tomislav said:static_cast also enables one to convert between types with implicit or
explicit conversion operators.
Of course, it works for pointer types only (just to clarify further).
Ian said:No, it works for any type with the same size. But it can't be used to
cast away const or volatile.
What is the difference between static cast and reinterpret cast?
No, it works for any type with the same size.
But it can't be used to cast away const or volatile.
No, you are wrong. reinterpret_cast only allows very limited
casts, like every other c++ cast does. It does, in particular
allow not to do
reinterpret_cast<unsigned char>('a'); // same size, eh?
And it does not allow casting to void* from another pointer,
or from void* to another pointer.
It's just what its name is: It *reinterprets* pointer
representations to integers, and object/member pointers among
themselves.
It also allows reinrerpreting arbitrary objects (disregarding
their size) by using references: "reinterpret_cast<unsigned
char&>(some_int)" is perfectly valid.
James said:Yes it does. It allows converting any object pointer type to
any other object pointer type (and in this context, void* is
considered an object type). Even if the two pointers have
different sizes, e.g. void* and int* on some machines. It also
allows converting any pointer to function type to any other
pointer to function type; it does not, however, allow converting
pointer to object (including void*) to or from pointer to
function, even if the two have the same size, as if often the
case (and is required by Posix, and I think at least indirectly
by Windows). Note that quite a few Unix compilers err here, and
accept the cast.
Agreed - as in the spirit of 3.10/15Yes, although in most cases, it's probably preferable to pass
through a pointer, e.g. reinterpret_cast< unsigned char* >(
&some_int ).
Here, size can affect the usability of the results:
reinterpret_cast< int& >( some_unsigned_char ) might be legal,
but using the results of the reinterpret_cast is undefined
behavior.
Johannes said:void* is not a "pointer to an object" - it's a "pointer to void". The
reinterpret_cast paragraph precisely only allows casting for two pointers
if both are pointers to object types. There is no mentioning of void
types.
The pointer type category is defined at 3.9.2/1 as "pointers to void or
objects or functions (including static members of classes) of a given
type", they are three different categories.
There was some work under way to define the word "object pointer" and use
it in reinterpret_cast to include "pointer to void" - but this work has
not yet been included into any draft. See http://www.open-
std.org/jtc1/sc22/wg21/docs/cwg_active.html#573
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.