Casting

Started by
12 comments, last by Landsknecht 21 years, 11 months ago
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]
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
Advertisement
WARNING: POSSIBLE MISINFORMATION

I thought dynamic cast was only for pointers.

END WARNING
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
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
Maybe its easier to search for when editing your code?

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
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
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
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?
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
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
const_cast can also cast away "volatile".

This topic is closed to new replies.

Advertisement