Jump to content
  • Advertisement

Archived

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

Landsknecht

Casting

This topic is 5895 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

I am trying to convert my thinking to use the C++ standard. But one thing that confuses the heck outta me is casting under the standards... What is the difference between these two?
    
float var1 = 2.3;

//This works fine

int var2 = (int)var1;

//This works as well

int var3 = static_cast<int>(var1);
    
What is the point other than a lot of extra typing? I just don't get it, I guess. Please explain this as it is confusing the hell outta me... Landsknecht edited to fix the error that masonium pointed out..... [edited by - landsknecht on April 30, 2002 9:36:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
OK... But the question still remains. What is the point of the C++ standard casts...

static_cast<>
const_cast<>
reinterpret_cast<>
dynamic_cast<>

How are they superior to the old style or simply, why should I type the extra bit... It just seems to be a redundant waste.

Landsknecht

Share this post


Link to post
Share on other sites
According to Wrox Press C++ Tutorial

static_cast<> - casts between related (like int to char) types at compile time (this is the most common)

reinterpret_cast<> - casts between unrelated types (like int to pointer)

const_cast<> - removes the const modifier

dynamic_cast<> - casts between pointers and references to polymorphic classes in an inheritance hierarchy

I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm

Share this post


Link to post
Share on other sites
I understand what they do... My question is why use them? I just don''t get why I should type 5 times as many characters to do the exact same thing?

Landsknecht

Share this post


Link to post
Share on other sites
quote:
Original post by Invader X
According to Wrox Press C++ Tutorial


(OT) Is that tutorial any good for someone learning C++? Is it complete or brief?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The point of the new casts is to seperate the different purposes that a casting operation can take. Since each cast only works in its particular situtation, it is harder to screw up your program.

static_cast<>()
This is the workhorse that you use in most situations. If there is no obvious conversion the compiler will notify you and if you try to cast away the const-ness of a variable the compiler will also throw an error. Never use a static_cast<>() unless you know the type that you are casting from.

dynamic_cast<>()
Dynamic casting is used when you are not sure what type a variable is. If that type is castable to the one you want it to then dynamic_cast<>() returns true. Otherwise it returns false. This allows you to do run-time type checking if that becomes necessary. It is relatively new so many compilers don''t implement it properly.

const_cast<>()
The const_cast<> is specifically used for changing whether a variable is const or not. It has few uses, but they are there so it is included.

reinterpret_cast<>()
This cast is used when you want to tell the compiler to shut up and treat the bits as if they were of the cast you set it to. This is the most dangerous cast, and should very rarely be used because it removes the safety net of the compiler''s type-checking facilities.

-D

Share this post


Link to post
Share on other sites
quote:
Original post by Arkainium
(OT) Is that tutorial any good for someone learning C++? Is it complete or brief?


It''s very helpful at times but I''d definately have at least one other text to help you if you plan to try and teach yourself from it.

I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!