static_cast 无条件转换,静态类型转换
dynamic_cast 有条件转换,动态类型转换,运行时检查类型安全
const_cast 去掉类型的const属性
reinterpret_cast 强制类型转换
#include <iostream>
using namespace std;
void test01()
{
//static_cast<>
//用于内置数据类型
//以及具有继承关系的指针或者引用
cout << "test01:" << endl;
int a = 97;
char b = static_cast<char>(a);
cout << "b: " << b << endl;
//dynamic_cast
//转换具有继承关系的指针或者引用
//转换前会检查 禁止父类指针转换成子类指针,小到大,可能造成越界
//const_cast
//基础类型的 指针 引用或者对象指针
//可以取消或者添加const(原数据的const不变)
int c = 10;
const int &d = c;
int & e = const_cast<int&>(d);
//reninterpret_cast 强制类型转换
//无关的指针 类型 包括函数指针都能转换
}
int main()
{
test01();
}