Original Post
🔒 Locked
dynamic_cast
Advertisement
reinterpret_cast is used to ... reinterpret the bit pattern as another type, without modification. While using static_cast to convert a float to an int would truncate, reinterpret_cast would just transfer the bits an read them as if it had been an int all along.
static_cast is a close relative of the C cast, with the exception that C casts can be reinterpret_casts in some cases. It is used when the actual types are known at compile-time.
dynamic_cast is used to safely downcast from a polymorphic Base (must have a virtual function) to a Derived type when you''re not sure the object really is of the Derived type. See below.
[ 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 ]
static_cast is a close relative of the C cast, with the exception that C casts can be reinterpret_casts in some cases. It is used when the actual types are known at compile-time.
dynamic_cast is used to safely downcast from a polymorphic Base (must have a virtual function) to a Derived type when you''re not sure the object really is of the Derived type. See below.
Base *pBase = new Base; // pointer to Base, Base object
Base *pDerived = new Derived; // pointer to Base, Derived object
Derived* pD0 = static_cast(pBase);
// BOOM ! pBase doesn''t point to a Derived object
Derived* pD1 = static_cast(pDerived):
// Ok. pDerived really pointed to a Derived object
Derived* pD0 = dynamic_cast(pBase);
// pD0 will be set to NULL, you can test for it.
Derived* pD1 = dynamic_cast(pDerived):
// Ok. pDerived really pointed to a Derived object
[ 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 ]
You''re probably right
[ 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 ]
[ 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