Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1753488
  • 博文数量: 413
  • 博客积分: 8399
  • 博客等级: 中将
  • 技术积分: 4325
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 10:44
文章分类

全部博文(413)

文章存档

2015年(1)

2014年(18)

2013年(39)

2012年(163)

2011年(192)

分类: C/C++

2011-08-28 18:11:08

我们知道,相对于C语言中粗暴的直接类型转换,C++中为每种必要的类型转换提供了分别不同的类型转换运算符关键字。在C中各种类型转换的运算符是一样的,这样的话,导致的后果是:当发生类型转换相关的bug时,我们不知到如何去查找,因为各种类型转换的运算符是一样的。但是在C++中,我们可以很容易地找到,因为在C++中各种类型转换的运算符关键字是不同的。

在C++中对于不同目的的类型转换提供不同的运算符,总结如下:

reinterpret_cast

只能用于指针的转换,将一个类型的指针转换为另一个类型的指针.或者将一个指针数值直接转换成整数。这种转换不会修改指针变量值存放格式(不改变指针变量值), 只需在编译时重新解释指针的类型就可做到. 

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <iomanip>

  4. using std::endl;
  5. using std::cout;
  6. using std::dec;
  7. using std::hex;

  8. int main()
  9. {
  10.         double pi = 3.1415926;
  11.         int *ppi = reinterpret_cast<int *>(&pi);
  12.         cout << pi << endl << *ppi << endl << endl;

  13.         class A{};
  14.         class B{};
  15.         A *pa = new A;
  16.         B *pb = reinterpret_cast<B*>(pa);

  17.         printf("pa: %p\npb: %p\n\n", pa, pb);
  18.         int i = 100;
  19.         int j = (int)&i;

  20.         printf("%p\n0x%x\n\n", &i, j);

  21.         cout << "reinterpret_cast(int*):" << endl;
  22.         int m = reinterpret_cast<int>(&i);
  23.         cout << "0x" << hex << m << endl << &i << endl << endl;

  24.         cout << "reinterpret_cast(int):" << endl;
  25.         int* pint = reinterpret_cast<int*>(100);
  26.         cout << "0x" << 100 << endl << pint << endl << endl;

  27.         return 0;
  28. }

运行结果为:

  1. 3.14159
  2. 1293080650

  3. pa: 0x902b008
  4. pb: 0x902b008

  5. 0xbff92004
  6. 0xbff92004

  7. reinterpret_cast<int>(int*):
  8. 0xbff92004
  9. 0xbff92004

  10. reinterpret_cast<int*>(int):
  11. 0x64
  12. 0x64

可以看到C++中的reinterpret_cast(int* )等价于C中的 int i = (int)(int* )     
                               reinterpret_cast(int )等价于C中的 int* pi = (int*)(int )     

注: reinterpret_cast字面的意思是“重新解释的转换”,它只是对指针进行重新的解释,所以它不会修改该指针的值。关于reinterpret_cast只要记住两点:
(1)它能将任何指针转换成任何指针,它能将指针转换成整数,也能将整数转换成指针
(2)它不会改变被转换的指针。它不能用对象之间的转换。

关于这两点其实很好理解:指针无非就是一个4字节或者8字节的数字而已,只不过这个数字是某个变量或对象的地址。对这个地址进行reinterpret_cast,就是对该地址所指向的变量或对象进行“重新解释”
而“指针与整数之间的转换”是因为它们大小相同都是4字节或者8字节。

const_cast

该函数用于去除指针变量的常量属性,将它转换为一个对应指针类型的普通变量。反过来,也可以将一个非常量的指针变量转换为一个常指针变量

这种转换是在编译期间做出的类型更改。

例:

  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;

  4. int main()
  5. {
  6.         const int i = 1;
  7.         const int *pci = &i;
  8.         // *pci = 10;    // error: assignment of read-only location ‘* pci’
  9.         cout << *pci << endl;

  10.         int *pk = const_cast<int*>(pci);   //相当于 int *pk = (int *)pci
  11.         *pk = 100;
  12.         cout << *pci << endl;
  13.         return 0;
  14. }

  15. const A* pca = new A;
  16. A* pa = const_cast<A*>(pca);  //相当于A* pa = (A*)pca;
出于安全性考虑,const_cast无法将非指针的常量转换为普通变量。

static_cast

该函数主要用于基本类型之间和具有继承关系的类型的对象或者变量之间的转换。

这种转换一般会更改变量的内部表示方式,因此,static_cast应用于指针类型转换没有太大意义

例:

  1. int i=0;
  2. double d = static_cast<double>(i);

  3. class Base{};
  4. class Derived : public Base{};
  5. Derived d;
  6. Base b = static_cast<Base>(d); 

注:动态只有static_cast能用于变量或对象的转换而不是它们的指针的转换。虽然是基本类型的变量和继承关系的对象。

dynamic_cast

它与static_cast相对,是动态转换。这种转换是在运行时进行转换分析的,并非在编译时进行,明显区别于上面三个类型转换操作。所以它对程序的性能是有所影响的!

该函数只能在继承类对象的指针之间或引用之间进行类型转换。进行转换时,会根据当前运行时类型信息,判断类型对象之间的转换是否合法。dynamic_cast的指针转换失败,可通过是否为null检测,引用转换失败则抛出一个bad_cast异常。

  1. class Base{};
  2. class Derived : public Base{};

  3. Derived *pd = new Derived;
  4. Base *pb = dynamic_cast<Base*>(pd);
  5. if (!pb)
  6.    cout << "dynamic_cast failed." << endl;

  7. class A(virtual ~A();) //有虚函数
  8. class B{}:

  9. A* pa = new A;
  10. B* pb = dynamic_cast<B*>(pa);
  11. if(pb)
  12.     cout << "dynamic_cast succeeded." << endl;

如果对无继承关系或者没有虚函数的类的对象指针进行转换、基本类型指针转换以及基类指针转换为派生类指针,都不能通过编译。

我们看到:

(1)各种类型的转换各司其职,有继承关系的对象或变量之间的转换用static_cast,而指针之间或者指针和整数之间的转换用reinterpret_cast,动态指针的转换用dynamic_cast。

(2)我们可以在基本类型的变量和继承关系的对象之间进行转换,我们可以将指针转换成整数,我们可以将任何类型的指针转换成任何类型的指针,我们可以去掉或加上const属性在指针上。

(3)利用C++中的reinterpret_cast、static_cast、dynamic_cast、const_cast等关键字,可以完全抛弃C中的强制转换。

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

上一篇:程序员装B指南

下一篇:友元函数与继承

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