static_cast、dynamic_cast、reinterpret_cast、const_cast

static_cast

1. 基础类型之间互转。如:float转成int、int转成unsigned int等

2. 指针与void*之间互转。如:float*转成void*、CBase*转成void*、函数指针转成void*、void*转成CBase*等

3. 派生类指针【引用】转成基类指针【引用】。如:Derive*转成Base*、Derive&转成Base&等

4. 非virtual继承时,可将基类指针【引用】转成派生类指针【引用】多继承时,会做偏移处理)。如:Base*转成Derive*、Base&转成Derive&等

该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。用于类层次结构中基类和子类之间指针或引用的转换。进行上行转换(把子类的指针或引用转换成基类表示)是安全的;进行下行转换(把基类指针或引用转换成子类表示)时,由于没有动态类型检查,所以是不安全的。

dynamic_cast  专门用于处理多态机制,对继承体系内的对象(类中必须含有至少一个虚函数)的指针【引用】进行转换,转换时会进行类型检查;vc在编译时要带上/EHsc /GR

如果能转换会返回对应的指针【引用】;不能转换时,指针会返回空,引用则抛出std::bad_cast异常(const std::bad_cast& e)

注:由于std::bad_cast类型定义在typeinfo头文件中,固需要#include<typeinfo>

另外,对于菱形非virtual继承、非public继承,转换引用时也会抛出std::bad_cast异常

reinterpret_cast  对指针【引用】进行原始转换,不做任何偏移处理(当然:多继承时,也不会做偏移处理

1. 将指针【引用】转换成整型。如:float*转成int、CBase*转成int、float&转成int、CBase&转成int等

float f1 = 1.0f; CBase o1;
int n1 = reinterpret_cast<int>(&f1);
int n2 = reinterpret_cast<int>(&o1);
int n3 = reinterpret_cast<int&>(f1);
int n4 = reinterpret_cast<int&>(o1);

2. 指针【引用】之间互转。如:float*转成int*、CBase&转成int&、CBase*转成CBase2*、CBase&转成CBase2&等

float f1 = 1.0f;  CBase1 o1;
int* n1 = reinterpret_cast<int*>(&f1);
int& n2 = reinterpret_cast<int&>(o1);
CBase2* o21 = reinterpret_cast<CBase2*>(&o1);
CBase2& o22 = reinterpret_cast<CBase2&>(o1);

reinpreter_cast 是二进制级别的类型转换,不安全

const_cast   去掉或增加constvolatile特性

C类型强制转换   形式:(type)objecttype(object)

最好是使用type(object);原因是:在某些编译器下,(type)object不会调用构造函数,而type(object)下则肯定会调用构造函数

C类型强制转换会按照以下顺序进行尝试转换:

a. const_cast
b. static_cast
c. static_cast, then const_cast
d. reinterpret_cast
f. reinterpret_cast, then const_cast



猜你喜欢

转载自blog.csdn.net/yufanghu/article/details/80855328
今日推荐