C++ 既支持 C 风格的类型转换,又有自己风格的类型转换。C 风格的转换格式很简单,但是有不少缺点:
C++ 风格完美的解决了上面两个问题。
下面分别来介绍这四种转换:static_cast、dynamic_cast、const_cast、reinterpert_cast,它们都是类模板。
(1)使用场景
#include
using namespace std;
class CBase // 基类(父类)
{
};
class CDerived : public CBase // 派生类(子类)
{
};
int main()
{
// 1. 使用static_cast在基本数据类型之间转换
float fval = 10.12;
int ival = static_cast<int>(fval); // float --> int
cout << ival << endl; // out: 10
// 2. 使用static_cast在有类型指针与void *之间转换
int *intp = &ival;
void *voidp = static_cast<void *>(intp); // int* --> void*
// cout << *voidp << endl; // error,voidp的大小未知
long *longp = static_cast<long *>(voidp);
cout << *longp << endl; // out: 10
// 3. 用于类层次结构中基类和派生类之间指针或引用的转换
// 上行转换(派生类---->基类)是安全的
CDerived *tCDerived1 = nullptr;
CBase *tCBase1 = static_cast<CBase*>(tCDerived1);
// 下行转换(基类---- > 派生类)由于没有动态类型检查,所以是不安全的
CBase *tCBase2 = nullptr;
CDerived *tCDerived2 = static_cast<CDerived*>(tCBase2); //不会报错,但是不安全
// 不能使用static_cast在有类型指针内转换
float *floatp = &fval; //10.12的addr
//int *intp1 = static_cast(floatp); // error,不能使用static_cast在有类型指针内转换
cout << *floatp << endl; // out: 10.12
}
/*
输出结果:
10
10
10.12
*/
(1)使用场景
#include
using namespace std;
class CBase // 基类(父类)
{
public:
// dynamic_cast在将父类cast到子类时,父类必须要有虚函数
virtual int test() { return 0; } // 一定要是 virtual
};
class CDerived : public CBase // 派生类(子类)
{
public:
int test() { return 1; }
};
int main()
{
CBase *p_CBase = new CBase; // 基类对象指针
CDerived *p_CDerived = dynamic_cast<CDerived *>(p_CBase); // 将基类对象指针类型转换为派生类对象指针
CBase i_CBase; // 创建基类对象
CBase &r_CBase = i_CBase; // 基类对象的引用
CDerived &r_CDerived = dynamic_cast<CDerived &>(r_CBase); // 将基类对象的引用转换派生类对象的引用
}
(1)使用场景
#include
using namespace std;
int main()
{
int value = 100;
const int *cpi = &value; // 定义一个常量指针
//*cpi = 200; // 不能通过常量指针修改值
// 1. 将常量指针转换为非常量指针,然后可以修改常量指针指向变量的值
int *pi = const_cast<int *>(cpi);
*pi = 200;
// 2. 将非常量指针转换为常量指针
const int *cpi2 = const_cast<const int *>(pi); // *cpi2 = 300; //已经是常量指针
const int value1 = 500;
const int &c_value1 = value1; // 定义一个常量引用
// 3. 将常量引用转换为非常量引用
int &r_value1 = const_cast<int &>(c_value1);
// 4. 将非常量引用转换为常量引用
const int &c_value2 = const_cast<const int &>(r_value1);
}
reinterpret 的英文含义有重新转换的含义,就相当于 C 语言中不相关类型的转换,强转。
(1)使用场景
#include
using namespace std;
int main()
{
int value = 100;
// 1. 用在任意指针(或引用)类型之间的转换
double *pd = reinterpret_cast<double *>(&value);
cout << "*pd = " << *pd << endl;
// 2. reinterpret_cast能够将指针值转化为整形值
int *pv = &value;
int pvaddr = reinterpret_cast<int>(pv);
cout << "pvaddr = " << hex << pvaddr << endl;
cout << "pv = " << pv << endl;
}
/*
输出结果:
*pd = -9.25596e+61
pvaddr = 8ffe60
pv = 008FFE60
*/
下面程序中,参数 pb 指向的是 B 类对象,pd1 的值不为0,而 pd2 的值为 0。
#include
using namespace std;
class B
{
int m_iNum;
virtual void foo() {};
};
class D:public B
{
char *m_szName[100];
};
void func(B* pb)
{
D *pd1 = static_cast<D *>(pb);
D *pd2 = dynamic_cast<D *>(pb);
cout << pd1 << endl; //00CFF7C0
cout << pd2 << endl; //00000000
}
int main()
{
B pb; //父类对象pb
cout << "&pb: " << &pb << endl; //&pb: 00CFF7C0
func(&pb);
return 0;
}
/*
输出结果:
&pb: 00CFF7C0
00CFF7C0
00000000
*/