C++语言级别提供的四种类型转换方式
int a = (int)b;
const_cast : 去掉(指针或者引用)常量属性的一个类型转换
static_cast : 提供编译器认为安全的类型转换(没有任何联系的类型之间的转换就被否定了)
reinterpret_cast : 类似于C风格的强制类型转换
dynamic_cast : 主要用在继承结构中,可以支持RTTI类型识别的上下转换
1.const_cast:
const int a = 10;
int *p1 = (int*)&a;
int *p2 = const_cast<int*>(&a);
******const_cast<这里面必须是指针或者引用类型 int* int&>
int b = const_cast<int>(a);
const必须对应,他只是去除const了,比如你是 const int 必须转换成int* 不能用double*
2.static_cast:
static_cast 基类类型 《=》 派生类类型 能不能用static_cast?当然可以!
转换成相关的并且安全的。
3.reinterpret_cast
类似于c风格暴力强制
4.dynamic_cast
这段代码应用场景就是第二个子类中有一个函数,当我们调用第二个子类,需要执行的时第二个构造函数。
class Base
{
public:
virtual void func() = 0;
};
class Derive1 : public Base
{
public:
void func() { cout << "call Derive1::func" << endl; }
};
class Derive2 : public Base
{
public:
void func() { cout << "call Derive2::func" << endl; }
// Derive2实现新功能的API接口函数
void derive02func()
{
cout << "call Derive2::derive02func" << endl;
}
};
void showFunc(Base *p)
{
// dynamic_cast会检查p指针是否指向的是一个Derive2类型的对象?
// p->vfptr->vftable RTTI信息 如果是,dynamic_cast转换类型成功,
// 返回Derive2对象的地址,给pd2;否则返回nullptr
// static_cast编译时期的类型转换 dynamic_cast运行时期的类型转换 支持RTTI信息识别
Derive2 *pd2 = dynamic_cast<Derive2*>(p);
if (pd2 != nullptr)
{
pd2->derive02func();
}
else
{
p->func(); // 动态绑定 *p的类型 Derive2 derive02func
}
}
int main()
{
Derive1 d1;
Derive2 d2;
showFunc(&d1);
showFunc(&d2);
return 0;
}