reinterpret_cast examples?

Started by
39 comments, last by Brother Bob 10 years, 11 months ago

If static_cast works I would use that since as you say it is less evil... reinterpret_cast should be for the case I mentioned earlier (i.e. don't change the address, just cast it to another type, AKA "compiler, I know what I am doing, trust me" cast).

EDIT: Multiple inheritance may mess that up though, I suppose. I can't be arsed reading a religious debate about it anyway ;)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement

Here's what the casts should have been called anyway:

static_cast: do_what_you_think_i_mean_and_by_the_way_one_thing_it_is_not_is_static_cast

reinterpret_cast: do_what_i_say_cast

const_cast: i_just_tried_to_annoy_you_by_lying_earlier_about_the_const_thing_cast

C-style cast: impossible_to_grep_for_cast. Java and C# really missed the boat on this one by not making it easier to do a search for casts. Visual Studio still hasn't got a "find any cast" search facility, which would be rather handy.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

wait...reinterpret cast....what does it exactly do,how does it work? does it just change the address?

what about static cast? does actually copy the bits to a new object?

Can someone explain how they work? getting really confused here

Reinterpret cast doesn't do anything ;) It just says to the compiler "here's this address, treat it as a pointer to this even though that would be illegal, I know what I'm doing".

static_cast can do lots of things like point to a different base object in the case of multiple inheritance or casting from a float to an int (completely changes the value, i.e. the very antithesis of "static").

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

If you want a really quick and basic summary, you can think of it like this:

  • reinterpret_cast is use to change the type but it won't change the underlying bits. For example, change the type of one pointer to another, or to put the value of a pointer in an integer.
  • static_cast is used to change the type and the logical value. For example, if you static_cast from a float to an integer, then the logical value is preserved; that is, if you cast from 1.0f, then the integer value is 1, even though the bits are completely different.

Anyway, I think the key lesson from this sermon is that if you have C++ code which uses casts it needs to be refactored into a lower-level interface which hides the gory details from you.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

with static_cast

You can explicitly convert a pointer of a type A to a pointer of a type B if A is a base class of B. If A is not a base class of B, a compiler error will result.

You may cast an lvalue of a type A to a type B& if the following are true:

  • A is a base class of B
  • You are able to convert a pointer of type A to a pointer of type B
  • The type B has the same or greater const or volatile qualifiers than type A
  • A is not a virtual base class of B

The result is an lvalue of type B.

A pointer to member type can be explicitly converted into a different pointer to member type if both types are pointers to members of the same class. This form of explicit conversion may also take place if the pointer to member types are from separate classes, however one of the class types must be derived from the other.

As if my head wasn't spinning...

Welcome to the wonderful world of C++ ;)

First bullet point is obvious. 2nd involves whether you have defined conversion operators which are public in the class I believe (EDIT: or accessible in the context of the cast e.g. private conversion operators ok in the same class, etc.). 3rd one requires const_cast. Not sure about the virtual inheritance 4th case, I've never used virtual inheritance.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

damn...just tested it,you can also convert a value of type b to the base class a without problems.Wondering why they didn't say that....

You don't need a cast for that... although it does slice the object if you pass or copy by value (EDIT: And virtual functions won't work if you cast a Derived to a Base class). In C you would need a cast which is why well written C++ requires less casting shenanigans than C.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement