In order to control conversions between classes, we have four specific casting operators: dynamic_cast, reinterpret_cast, static_cast and const_cast.
Format:
- dynamic_cast <new_type> (expression)
-
reinterpret_cast <new_type> (expression)
-
static_cast <new_type> (expression)
-
const_cast <new_type> (expression)
Dynamic cast:
dynamic_cast can be used only with pointers and references to
objects. Its purpose is to ensure that the result of the type conversion
is a valid complete object of the requested class.
- #include <iostream>
-
#include <exception>
-
using namespace std;
-
-
class CBase {virtual void dummy() {}};
-
class CDerived: public CBase {int a;};
-
-
int
-
main(void)
-
{
-
try {
-
CBase *pba = new CDerived;
-
CBase *pbb = new CBase;
-
CDerived * pd;
-
-
pd = dynamic_cast<CDerived*>(pba);
-
if (pd == 0) cout << "NULL pointer on first type-cast" << endl;
-
-
pd = dynamic_cast<CDerived*>(pbb);
-
if (pd == 0) cout << "NULL pointer on second type-cast" << endl;
-
} catch (exception &e) {
-
cout << "Exception: " << e.what() << endl;
-
}
-
-
return (0);
-
}
Static cast:
static_cast can perform conversions between pointers to related
classes, not only from the derived class to its base, but also from a
base class to its derived. This ensures that at least the classes are
compatible if the proper object is converted, but no safety check is
performed during runtime to check if the object being converted is in
fact a full object of the destination type. Therefore, it is up to the
programmer to ensure that the conversion is safe. On the other side, the
overhead of the type-safety checks of dynamic_cast is avoided.
static_cast can also be used to perform any other non-pointer conversion that could also be performed implicitly or any conversion between classes with explicit constructors or operator functions。
Reinterpret cast:
reinterpret_cast converts any pointer type to any other pointer
type, even of unrelated classes. The operation result is a simple
binary copy of the value from one pointer to the other. All pointer
conversions are allowed: neither the content pointed nor the pointer
type itself is checked.
It can also cast pointers to or from integer types. The format in which
this integer value represents a pointer is platform-specific. The only
guarantee is that a pointer cast to an integer type large enough to
fully contain it, is granted to be able to be cast back to a valid
pointer.
const cast:
This type of casting manipulates the constness of an object, either to be set or to be removed.
- #include <iostream>
-
using namespace std;
-
-
void
-
print(char *str)
-
{
-
cout << str << endl;
-
}
-
-
int
-
main(void)
-
{
-
const char *c = "hello";
-
print(const_cast<char *>(c));
-
-
return (0);
-
}
typeid:
typeid allows to check the type of an expression.
Format:
- #include <iostream>
-
#include <typeinfo>
-
#include <exception>
-
using namespace std;
-
-
class CBase {virtual void f(){}};
-
class CDerived: public CBase{};
-
-
int
-
main(void)
-
{
-
try {
-
CBase *a = new CBase;
-
CBase *b = new CDerived;
-
cout << "a is:" << typeid(a).name() << endl;
-
cout << "b is:" << typeid(b).name() << endl;
-
cout << "*a is:" << typeid(*a).name() << endl;
-
cout << "*b is:" << typeid(*b).name() << endl;
-
} catch (exception &e) {
-
cout << "Exception" << e.what() << endl;
-
}
-
-
return (0);
-
}
阅读(883) | 评论(0) | 转发(0) |