Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jcdenton999

Casting in C++

This topic is 5656 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there The book I''m reading (Thinking in C++) has a section about casting. I don''t really understand what it''s all about. What is the point of casts. I don''t understand that, but the author is going on about static_cast, reinterpret_cast, and const_cast. Why would you use casts -- and what is the difference between them all. Thx

Share this post


Link to post
Share on other sites
Advertisement
Casting could be translated into, change/transform. Perhaps you are familiar with the command, malloc. This function will return a VOID* to the newly allocated memory block.

Consider the following scenario:

int *intpointer;

intpointer = (int *) malloc( nSize, sizeof( nSize ) );

Above (int *) is your caster. As I mentioned before, malloc will normally return a VOID* type, but with (int *) you will CAST the returned type to be a pointer to integer.



---
Christopher Strömblad
Hobby programmer

Share this post


Link to post
Share on other sites
quote:
Original post by snail0r
intpointer = (int *) malloc( nSize, sizeof( nSize ) );



I think you mean
intpointer = malloc(nSize * sizeof intpointer);

Casts should probably be really rare. You''re basically telling the compiler "I know this looks bad, but I know what I''m doing". For instance, you''ll probably never cast malloc. If you''re in C, then you don''t need to cast void * (and doing so can mask errors). If you''re in C++, why aren''t you using new?

From what I can gather, static_cast is what you should "try" first. I think it''s like saying "these types are compatible even though you (the compiler) don''t think they are". reinterpret_cast is like saying "take these bits and treat them as if they were the bits of some other type". This can cause lots of problems because the internal representations of, say, an int and a pointer need not be compatible so casting between them like this can have interesting results. const_cast toggles the const-ness of a type. Having to use this probably shows an error in your design (if you need to modify it, why''s it const? If it must be const but you have to modify it, look into the mutable keyword). I don''t recall what dynamic_cast does.

C style casts, such as (int *), are like a combination of static_cast and reinterpret_cast. Basically, keep trying things untill you can use it as the type you''re casting it to.

Share this post


Link to post
Share on other sites
Ops, thanks for the correction. Just saw that I completely mixed up calloc and malloc, my bad. Shame on me, time to build a shame on me box.



---
Christopher Strömblad
Hobby programmer

Share this post


Link to post
Share on other sites
quote:
Original post by jcdenton999
Hey there

The book I''m reading (Thinking in C++) has a section about casting. I don''t really understand what it''s all about. What is the point of casts. I don''t understand that, but the author is going on about static_cast, reinterpret_cast, and const_cast.

Why would you use casts -- and what is the difference between them all.

Thx


Without going to depth on the difference (i.e see the other posts) Here is a simple scenario on using cast. It is just an example and should not be taken that seriously. Let''s say you have a float x and this x has the value 3.5f. You also have an int y with the value 10. Now, for some reason you need the y value to replace the x value. what you can do is: x = (float)y;
That is, convert the value in y from being int into a float that now can be stored in x. As I said, this is just one simple case but this can be seen as a quite common reason to why you need casts. Sometimes you really need to convert between formats and should not be seen as a design flaw. I would advice you to place a lot of text around conversions that tells why you need to do it. Not so much for debugging reasons as to others(or yourself) reading the source after a long time.

____________________________________________________________
Try RealityRift at www.planetrift.com
Feel free to comment, object, laugh at or agree to this. I won''t engage in flaming because of what I have said.
I could be wrong or right but the ideas are mine.

Share this post


Link to post
Share on other sites
Casts aren''t all that bad

Depends on the cast type (for C++ that is) dynamic_cast can be good if you''re trying to determine what that pntr to a base object is in a function ... for example

Function(BaseClass* Object)
{
ClassA* pObjA = dynamic_cast(Object);
ClassB* pObjB = dynamic_cast(Object);
if(pObjA)
Do_Something_Only_A_can_do(...);
if(pObjB)
Do_Something_Only_A_can_do(...);
else
Called by something we don''t care about so ignore
it or call Do_default()
}

Now you might say it is bad design, why not go virtual and all the rest, but if you inherit a *lot* of classes from a common base and those inherited classes are very different in nature then you''ll have one hellava list of virtuals

I agree though that casts should b used with extreme care, especially if you''re a newbie

Share this post


Link to post
Share on other sites
Will pObjB be NULL if the dynamic_cast fails? That is if I passed in an pointer to an object of type ClassA to the function.

Share this post


Link to post
Share on other sites
quote:
Original post by jcdenton999
Why would you use casts -- and what is the difference between them all.
Casts effect type conversion. Sometimes you need to convert data from being treated as one type to being treated as another, especially when you use code/libraries written by other people (eg Win32 API, DirectX).

  • const_cast: Used to convert const (constant, immutable, cannot be changed) values to volatile (modifiable) and vice versa. A failed attempt at a const_cast is a compile-time error.

  • static_cast: Used to convert related integral types - between int, char, long and their variants. A failed attempt at a static_cast is a compile-time error.

  • dynamic_cast: Used to convert up and down an inheritance hierarchy. A failed dynamic_cast returns null (0) at runtime, and thus is used by some as a means of runtime type identification (RTTI), as in stew''s example above.

  • reinterpret_cast: Attempts to convert any type to any other type of the same size. reinterpret_casts are unsafe, so don''t use them unless you know what you''re doing - an example would be casting a pointer to a long to interact with Win32, then casting it back to retrieve the pointer. I''m not sure of the failure conditions and returns of reinterpret_cast; since it will probably only fail if the types are not of the same size, which can be determined at compile-time, its failure is most likely a compile-time error.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!