C语言的类型转换
C语言的类型转换分为隐式类型的转换和强制类型的转换。 隐式类型转换:(适用于相近类型的转换)
int main()
{
int i = 10;
double d = 10.111;
i = d;//发生了隐式类型转换
int n = 10;
size_t pos = 0;
while (n-- >= pos)//由于隐式类型的转换,n的类型被转换成了size_t,所以会发生死循环
{
cout << n << endl;
}
return 0;
}
强制类型转换:(适用于不相关类型的转换)
int main()
{
int i = 10;
int* p = (int*)i;//强制类型转换
return 0;
}
C++的强制类型转换
因为C语言的隐式类型转换坑很大,标准C++为了加强类型转换的可视性,引入了四种命名的强制类型转换操作符:
static_cast(相近类型的转换)
static_cast用于静态转换,它对应的是C语言中的隐式类型转换。编译器隐式执行的任何类型转换都可用static_cast,但是它不能用于两个不相关的类型进行转换。
void test1()
{
double d = 12.34567;
int a = static_cast<int>(d);//相当于一个模板,做的是相近类型的隐式转换
cout << a << endl;
}
reinterpret_cast(不相关类型的转换)
reinterpret_cast通常用于不相关类型的转换。
void test2()
{
int i = 10;
int* p = reinterpret_cast<int*>(i);//无关类型的转换
}
无关类型的转换很强大,它还可以这样(但是不建议使用):
typedef void(*FUNC)();
int DoSomething(int i)
{
cout << "DoSomething" << endl;
return 0;
}
void test3()
{
// reinterpret_cast可以编译器以FUNC的定义方式去看待DoSomething函数
// 所以非常的BUG,下面转换函数指针的代码是不可移植的,所以不建议这样用
// C++不保证所有的函数指针都被一样的使用,所以这样用有时会产生不确定的结果
FUNC f = reinterpret_cast< FUNC>(DoSomething);
f();
}
const_cast(去除const属性的转换)
const_cast最常用的用于删除变量的const属性,方便赋值。
void test4()
{
volatile const int i = 2;//保持内存的可见性
int* p = const_cast<int*>(&i);//去除i的const属性
*p = 3;
cout << i << endl;
}
const_cast +reinterpret_cat---->等价于C语言中的强制类型转换。
dynamic_cast
dynamic_cast只能用于多态,常用于将一个父类对象的指针转换为子类对象的指针或引用.
- 向上转:子类对象指针->父类指针/引用(不需要转换,赋值兼容规则)
- 向下转:父类对象指针->子类指针/引用(用dynamic_cast转是安全的)
class A
{
public:
virtual void f(){}
};
class B : public A
{};
void fun(A* pa)
{
B* pb1 = static_cast<B*>(pa);
B* pb2 = dynamic_cast<B*>(pa);// dynamic_cast会先检查是否能转换成功,能成功则转换,不能则返回nullptr
cout << "pb1:" << pb1 << endl;
cout << "pb2:" << pb2 << endl;
}
void test5()
{
A a;
B b;
fun(&a);
fun(&b);
}
需要注意的是:
- dynamic_cast只能用于含有虚函数的类。
- dynamic_cast会识别父类指针是指向父类,还是指向子类。
- dynamic_cast会先检查是否能转换成功,能成功则转换,不能则返回nullptr。
为什么C++需要四种类型转换
- C语言的隐式类型转化有些情况下可能会出问题;
- 显式类型转换将所有情况混合在一起,代码不够清晰。