tcache Contact Me ----------- AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
🔒 Locked
Casts?
Advertisement
Original Post
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)
tcache Contact Me ----------- AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
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
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
So reinterpret and static are basically the same thing?
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
You could use
Update GameDev.net system time campaign:
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''
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 ]
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 ]
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)
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
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 ]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement