Skip to main content
gamedev.net
🔒 Locked

Casts?

Started by Tazel Jan 26, 2003 at 12:58 AM 11 replies 300+ views
Original Post
Tazel
Tazel
What are the differences between all the different casts? static_cast, reinterpret_cast, etc. How many others are there? How do they differ?
tcache Contact Me ----------- AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
civguy
civguy
ever heard of google?
Neophyte
Neophyte
There are currently 4 types of cast in C++:
static_cast
const_cast
dynamic_cast
reinterpret_cast

static_cast is used to cast from one type to another type.
const_cast is used to cast away "const-ness" or "volatile-ness" of a type
dynamic_cast is used to cast between base and derived classes
reinterpret_cast is used to perform type conversions on unrelated types.

For more information, there a decent article here.

-Neophyte
Tazel
Tazel
So reinterpret and static are basically the same thing?



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Chozo
Chozo
static_cast is a checked cast, reinterpret_cast is not. So the former gives you type-safety, the latter doesn''t.
dalleboy
dalleboy
You could use reinterpret_cast to cast between unrelated types, but not static_cast.

  
class Foo
{
public:
virtual ~Foo()
{
}
};

class Bar : public Foo
{
};

class Baz : public Foo
{
};

class Qux
{
};

int main()
{
Foo* pFoo = new Bar();
// Related type cast of the actual type.

Bar* pDynamicBar = dynamic_cast<Bar*>(pFoo); // Safe: Returns pointer to Bar object.

Bar* pStaticBar = static_cast<Bar*>(pFoo); // Safe: Returns pointer to Bar object.

Bar* pReinterpretBar = reinterpret_cast<Bar*>(pFoo); // Safe: Returns pointer to Bar object.

// Related type cast.

Baz* pDynamicBaz = dynamic_cast<Baz*>(pFoo); // Safe: Returns 0.

Baz* pStaticBaz = static_cast<Baz*>(pFoo); // Unsafe: Returns pointer to Bar object.

Baz* pReinterpretBaz = reinterpret_cast<Baz*>(pFoo); // Unsafe: Returns pointer to Bar object.

// Unrelated type cast.

Qux* pDynamicQux = dynamic_cast<Qux*>(pFoo); // Safe: Returns 0.

Qux* pStaticQux = static_cast<Qux*>(pFoo); // Compiler error: cannot convert from ''Foo*'' to ''Qux*''.

Qux* pReinterpretQux = reinterpret_cast<Qux*>(pFoo); // Unsafe: Returns pointer to Bar object.

}




Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Fruny
Fruny
static_cast will do a sensible conversion if any exists, otherwise will fail at compile time.

reinterpret_cast will preserve the bit pattern, simply reinterpreting it as a different type (struct A to struct B of same size). It will fail if the types have different sizes or are otherwise incompatible.



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
dalleboy
dalleboy
Only use reinterpret_cast if you know what you are doing. Use static_cast with caution, as it may ''lie'' to you. dynamic_cast is the safest one, but it''s slower than static_cast.



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Tazel
Tazel
When you''re casting a float and an int, is that related because it uses only numbers?



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
dalleboy
dalleboy
quote:
Original post by Tazel
When you''re casting a float and an int, is that related because it uses only numbers?

Yes, static_cast would do.



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Kippesoep
Kippesoep
C-style casts are reinterpret_cast-s

Kippesoep
Fruny
Fruny
quote:
Original post by Kippesoep
C-style casts are reinterpret_cast-s



They can either be seen as

- static_casts : float f = 3.5f; int i = (int)f;
- reinterpret_casts : float f = 3.5f; int i = *(int*)&f
- const_casts, temporary variable creation (constructor calls)

which is why they should be avoided in C++ code : it isn''t clear *what* a given cast does, and minor modifications of an expression can completely change its meaning.

The only thing C casts cannot ever be is dynamic_casts.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.