reinterpret_cast vs. static_cast 
Author Message
 reinterpret_cast vs. static_cast

Quote:

>Take a look at the following code:

>class IMulti1
>{
>public: virtual void foo() {}
>};

>class IMulti2
>{
>public: virtual void foo() {}
>};

>class CClass : public IMulti1, public IMulti2
>{
>public: virtual void foo() {}
>};

>int main()
>{
> CClass cclass;

> IMulti2* pMultiA = (IMulti2*)&cclass;
> IMulti2* pMultiB = reinterpret_cast< IMulti2* >(&cclass);
> IMulti2* pMultiC = static_cast< IMulti2* >(&cclass);
> IMulti2* pMultiD = dynamic_cast< IMulti2* >(&cclass);
>}

>All of the IMulti2 pointers will be initialized properly except the
>reinterpret_cast one.  It points to a different location who's vtable is the
>IMulti1 vtable.

>Why?

The effect of reinterpret cast is highly implementation
dependent.  However, in most implementations, it
means:  Take this collection of bits that was once
a pointer of some kind or an int, and pretend it is
a pointer of some other kind or an int.  There is no
implied change to the value of the bits.  (A change
could occur, but is is not required.)

Quote:
>I thought that reinterpret_cast was supposed to do the same thing as a
>straight cast (pMultiA).

What you call a "straight cast" becomes whatever
combination of reinterpret_cast, const_cast, and
static_cast is needed to get from the cast source
to the cast result.  So, sometimes your "straight
cast" becomes a static_cast, which is distinctly
different from reinterpret_cast.

Quote:
>Another anomaly I've discovered here is that the straight cast will do the
>same thing as the reinterpret_cast if the compiler doesn't have access to the
>full declaration of CClass (this at least makes some sense):

This is a consequence of the "becomes whatever
combination ..." rule.  C-style casts (your "straight
cast") are evil because they can silently change in
meaning depending on nearly invisible context.

Quote:
>class CClass;  // forward reference

>class Other
>{
>   CClass* m_pCClassPtr;
>public:
>   IMulti2* GetPtr()
>      { return (IMulti2*)m_pCClassPtr; }
>};

Best advice re C-style casts: Just say no.

--Larry Brasfield
Above opinions may be mine alone.



Sun, 01 Apr 2001 03:00:00 GMT  
 reinterpret_cast vs. static_cast

Quote:
>I thought that reinterpret_cast was supposed to do the same thing as a
>straight cast (pMultiA).

This is not really correct.  A reinterpret_cast is more like "convert,
dammit" and very little about what it does is defined.

Quote:
>Another anomaly I've discovered here is that the straight cast will do
the
>same thing as the reinterpret_cast if the compiler doesn't have access
to the
>full declaration of CClass (this at least makes some sense):

The old C-style "straight cast" had several different meanings; the new
cast operators are intended to explicitly take one meaning apiece, and
reinterpret_cast is the one whose meaning is (free translation) "convert
these types despite the fact that they may be entirely unrelated and the
type conversion is obviously meaningless".  You might say that
reinterpret_cast should be used when you're trying to actually break the
type system, rather than merely bending it a little like the others do.
The "anomaly" you've encountered is the problem the new operators
address: a C-style cast has no single meaning, and the compiler will
attempt successively less safe conversions when you use one in an
attempt to give you the conversion you asked for, ending with a
reinterpret_cast.  The new operators allow us to say exactly what we
mean when we ask for a type conversion.

What reinterpret_cast actually does is implementation-defined.  About
the only thing guaranteed with reinterpret_cast is that if you cast back
you'll get the same value you had before.  Most (all?) implementations
just give you the bit pattern back untouched.  In your case, this leaves
you with a pointer whose type is the selected base, but whose vtable is
that of another class; many bad things can happen from there.  This is
one of the many reasons to avoid reinterpret_cast where possible.

--
Nate Lewis, MCSD



Sun, 01 Apr 2001 03:00:00 GMT  
 reinterpret_cast vs. static_cast

Quote:

>You say that a straight cast is unpredictable, but exactly what can you
>predict about static_cast?  I did note that the compiler will not allow you
>to use static_cast without the full declaration of a type, so perhaps it's
>simply safer because of this, or is there more to it?

It is safer because it can only convert a value according
to well-defined rules or convert a pointer in ways that
correspond to legitimate relationships among classes.
It is safer because it can never automatically become
a reinterpret_cast.

Here is part of what recently wrote about static_cast:

static_cast<new_type>(value_expression)
  means convert the value expression, which must be
  an r-value, to a value of the new type using the
  defined (by C++ or code) conversion operators.
  If no legal conversion sequence is defined, then
  a compilation error results.  This can result in
  (accidental, silent) slicing for object upcasts.

static_cast<new_type *>(pointer_expression)
  means treat the pointer value as if it points to
  the new type, applying whatever adjustments may be
  required by traversing an inheritance hierarchy.
  If no legal traversal is defined, then this will
  result in a compilation error.  (This will never
  result in a reinterpret_cast.)

static_cast<new_type &>(reference_expression)
  same as for pointers, with reference semantic

--Larry Brasfield
Above opinions may be mine alone.



Mon, 02 Apr 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. reinterpret_cast vs. static_cast

2. Implementation of QueryInterface(): static_cast vs. reinterpret_cast

3. Implementation of QueryInterface(): static_cast vs. reinterpret_cast

4. static_cast OR reinterpret_cast

5. reinterpret_cast / static_cast

6. reinterpret_cast and static_cast

7. static_cast, dynamic_cast, reinterpret_cast

8. static_cast, dynamic_cast and reinterpret_cast

9. static_cast<> vs. reinterpret_cast<>

10. static_cast vs traditional casting

11. dynamic_cast vs. static_cast

12. Difference of static_cast<>,dynamic_cast<> and reinterpret_cast<>

 

 
Powered by phpBB® Forum Software