Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370691
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-19 13:51:48

In order to control conversions between classes, we have four specific casting operators: dynamic_cast, reinterpret_cast, static_cast and const_cast.
Format:
  1. dynamic_cast <new_type> (expression)
  2. reinterpret_cast <new_type> (expression)
  3. static_cast <new_type> (expression)
  4. 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.
  1. #include <iostream>
  2. #include <exception>
  3. using namespace std;

  4. class CBase {virtual void dummy() {}};
  5. class CDerived: public CBase {int a;};

  6. int
  7. main(void)
  8. {
  9.         try {
  10.                 CBase *pba = new CDerived;
  11.                 CBase *pbb = new CBase;
  12.                 CDerived * pd;

  13.                 pd = dynamic_cast<CDerived*>(pba);
  14.                 if (pd == 0) cout << "NULL pointer on first type-cast" << endl;

  15.                 pd = dynamic_cast<CDerived*>(pbb);
  16.                 if (pd == 0) cout << "NULL pointer on second type-cast" << endl;
  17.         } catch (exception &e) {
  18.                 cout << "Exception: " << e.what() << endl;
  19.         }

  20.         return (0);
  21. }

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.
  1. #include <iostream>
  2. using namespace std;

  3. void
  4. print(char *str)
  5. {
  6.         cout << str << endl;
  7. }

  8. int
  9. main(void)
  10. {
  11.         const char *c = "hello";
  12.         print(const_cast<char *>(c));

  13.         return (0);
  14. }

typeid:
typeid allows to check the type of an expression.
Format:
  1. typeid (expression)

  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <exception>
  4. using namespace std;

  5. class CBase {virtual void f(){}};
  6. class CDerived: public CBase{};

  7. int
  8. main(void)
  9. {
  10.         try {
  11.                 CBase *a = new CBase;
  12.                 CBase *b = new CDerived;
  13.                 cout << "a is:" << typeid(a).name() << endl;
  14.                 cout << "b is:" << typeid(b).name() << endl;
  15.                 cout << "*a is:" << typeid(*a).name() << endl;
  16.                 cout << "*b is:" << typeid(*b).name() << endl;
  17.         } catch (exception &e) {
  18.                 cout << "Exception" << e.what() << endl;
  19.         }

  20.         return (0);
  21. }


阅读(889) | 评论(0) | 转发(0) |
0

上一篇:异常

下一篇:预处理

给主人留下些什么吧!~~