- reinterpret_cast:可以用于任意类型的指针之间的转换,对转换的结果不做任何保证
- dynamic_cast:这种其实也是不被推荐使用的,更多使用static_cast,dynamic本身只能用于存在虚函数的父子关系的强制类型转换,对于指针,转换失败则返回nullptr,对于引用,转换失败会抛出异常
- const_cast:对于未定义const版本的成员函数,我们通常需要使用const_cast来去除const引用对象的const,完成函数调用。另外一种使用方式,结合static_cast,可以在非const版本的成员函数内添加const,调用完const版本的成员函数后,再使用const_cast去除const限定。
- static_cast:完成基础数据类型;同一个继承体系中类型的转换;任意类型与空指针类型void* 之间的转换。
隐式类型转换
首先,对于内置类型,低精度的变量给高精度变量赋值会发生隐式类型转换
其次,对于只存在单个参数的构造函数的对象构造来说,函数调用可以直接使用该参数传入,编译器会自动调用其构造函数生成临时对象。
#include<iostream>
using namespace std;
int main() {
int n = 5;
float f = 10.0;
// 本质上发生了隐式转换
f = n;
///////////// static_cast:基本等价于隐式转换,只是明确写出来,强调进行了类型转换使得代码的可读性更好
// 低风险转换
// 整型与浮点型
f = static_cast<float>(n);
// 字符与整型
char ch = 'a';
n = static_cast<int>(ch);
// void* 指针的转换
void* p = nullptr;
int* pN = static_cast<int*>(p);
// 高风险转换
int kk;
char* p;
// p = kk; //隐式转换转不了
// char *k = static_cast<char*>(kk); // 也隐式转换转不了
/////////// 基类与派生类之间的转换
// 父类转子类:不安全
// pSon = pFather; // 不能通过
// pSon = static_cast<pSon*> pFather; // 可以通过,但不安全
// 子类转父类:安全 ,都能通过
// pFather = pSon;
// pFather = static_cast<pFather*> pSon;
//////////////// dynamic_cast:虚函数的基类与派生类的之间的指针或者引用的转换。它能够在运行时,检测出被转换的指针的类型,必须要有虚函数
/// 在运行时检测父类转换为子类是不安全的,会返回nullptr
pFather = static_cast<pSon>(pSon);
pSon = dynamic_cast<pFather*>(pFather);
if (pSon == nullptr) {
...
}
// 1. 有虚函数且基类指针转化为派生类指针时(向下转换),必须使用。
//////// reinterpret_cast:相当于显示强转,提高程序可读性,适用于各种危险的转换方式,后果自负
// eg.int -> int*
int n = 1;
int* p = (int*)n;
int* p = reinterpret_cast<int*>(n);
// eg.int*->char*
char* ch = reinterpret_cast<char*>(p);
///////// const_cast:常用于const属性和volatile属性(多线程编程中强制编译器不优化)的去除,只针对指针、引用、或者this指针。
const int n = 5;
const string s = "hello";
int* k = const_cast<int*>(&n);
*k = 123;
int& kRef = const_cast<int&>(n);
kRef = 345;
// 对于常成员函数(不能修改成员变量的值)
}