#### Archived

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

# Casting

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

## 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 on other sites
WARNING: POSSIBLE MISINFORMATION

I thought dynamic cast was only for pointers.

END WARNING

##### Share on other sites
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 on other sites
Maybe its easier to search for when editing your code?

##### 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...

##### 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 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 on other sites
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 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...

##### Share on other sites
const_cast can also cast away "volatile".

##### Share on other sites
You know. AP always confuses me. I can understand why people use AP when they are flaming, but why when they are actually being remarkably informative?

The AP up there actually had my answer. It makes sense now. Still seems like to much extra typing, but at least I understand now.

Thanks AP,
Landsknecht

##### Share on other sites
hes probably too lazy to log in. or perhapse doesnt trust the location he posts at (work,a net cafe, or something)

i dunno. maybe its just best to let him explain.

or maybe its a she.... and we would always comment on how a girl knows so much etc etc. maybe "she" has had enough of the sexism.

i also just noticed that it allows AP to get out of viewing profile and sending mail to regarding the post.

[edited by - noober on April 30, 2002 12:51:06 AM]

##### Share on other sites
quote:
Original post by Landsknecht
The AP up there actually had my answer. It makes sense now. Still seems like to much extra typing, but at least I understand now.

There''s even a reason for the extra typing. Casts have been proven over time to be a constant source of bugs. Therefore, it makes sense to make them easy to find in the source; the c-style casting syntax is far too subtle.

[C++ FAQ Lite | ACCU | Boost | Python]

##### Share on other sites
quote:
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.

No it doesn''t.

dynamic_cast<>() on pointers returns NULL if the cast was bad, or a valid pointer if it was OK.

dynamic_cast<>() on references throws a bad_cast exception if the cast was bad, or a valid reference if it was OK.