C++四种类型转换运算符的使用方法

1、reinterpret_cast

该函数将一个类型的指针转换为另一个类型的指针.

这种转换不用修改指针变量值存放格式(不改变指针变量值),只需在编译时重新解释指针的类型就可做到.
reinterpret_cast 可以将指针值转换为一个整型数,但不能用于非指针类型的转换.
例:

1
2
3
4
5
6
7
8
9
10
11
12
13
//基本类型指针的类型转换
double d=9.2;
double* pd = &d;
int *pi = reinterpret_cast<int*>(pd);  //相当于int *pi = (int*)pd;

//不相关的类的指针的类型转换
class A{};
class B{};
A* pa = new A;
B* pb = reinterpret_cast<B*>(pa);   //相当于B* pb = (B*)pa;

//指针转换为整数
long l = reinterpret_cast<long>(pi);   //相当于long l = (long)pi;

2、const_cast

该函数用于去除指针变量的常量属性,将它转换为一个对应指针类型的普通变量。反过来,也可以将一个非常量的指针变量转换为一个常指针变量。
这种转换是在编译期间做出的类型更改。
例:

1
2
3
4
5
6
const int* pci = 0;
int* pk = const_cast<int*>(pci);  //相当于int* pk = (int*)pci;


const A* pca = new A;
A* pa = const_cast<A*>(pca);     //相当于A* pa = (A*)pca;

出于安全性考虑,const_cast无法将非指针的常量转换为普通变量。

3、static_cast

该函数主要用于基本类型之间和具有继承关系的类型之间的转换。
这种转换一般会更改变量的内部表示方式,因此,static_cast应用于指针类型转换没有太大意义。

主要有如下几种用法:

  • 用于类层次结构中基类和子类之间指针或引用的转换。

  • 进行上行转换(把子类的指针或引用转换成基类表示)是安全的;

  • 进行下行转换(把基类指针或引用转换成子类指针或引用)时,由于没有动态类型检查,所以是不安全的。

  • 用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。

  • 把void指针转换成目标类型的指针(不安全!!)

  • 把任何类型的表达式转换成void类型。

例:

1
2
3
4
5
6
7
8
9
//基本类型转换
int i=0;
double d = static_cast<double>(i);  //相当于 double d = (double)i;

//转换继承类的对象为基类对象
class Base{};
class Derived : public Base{};
Derived d;
Base b = static_cast<Base>(d);     //相当于 Base b = (Base)d;

4、dynamic_cast

用法:dynamic_cast < type-id > ( expression )
说明:该运算符把expression转换成type-id类型的对象。Type-id必须是类的指针、类的引用或者void *;
如果type-id是类指针类型,那么expression也必须是一个指针,如果type-id是一个引用,那么expression也必须是一个引用。
它与static_cast相对,是动态转换。
这种转换是在运行时进行转换分析的,并非在编译时进行,明显区别于上面三个类型转换操作。
该函数只能在继承类对象的指针之间或引用之间进行类型转换。进行转换时,会根据当前运行时类型信息,判断类型对象之间的转换是否合法。dynamic_cast的指针转换失败,可通过是否为null检测,引用转换失败则抛出一个bad_cast异常。
例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Base{};
class Derived : public Base{};


//派生类指针转换为基类指针
Derived *pd = new Derived;
Base *pb = dynamic_cast<Base*>(pd);

if (!pb)
    cout << "类型转换失败" << endl;

//没有继承关系,但被转换类有虚函数
class A
{

virtual ~A(){};

}   //有虚函数
class B{}:
A* pa = new A;
B* pb  = dynamic_cast<B*>(pa);

如果对无继承关系或者没有虚函数的对象指针进行转换、基本类型指针转换以及基类指针转换为派生类指针,都不能通过编译。

应用:

解决因引用或指针使用了父类而实质为子类的对象访问不到自己的private变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <cmath> 

class Point2D
{
private:
double x;
double y;

public:
Point2D() {
x = 0;
y = 0;
}

double getX() {
return x;
}

double getY() {
return y;
}

void setX(double x) {
this->x = x;
}

void setY(double y) {
this->y = y;
}

virtual double distance(Point2D & point2) {
double dx = x - point2.x;
double dy = y - point2.y;
return sqrt(dx * dx + dy * dy);
}

};

class Point3D : public Point2D
{
private:
double z;
public:
Point3D():z(0), Point2D(){}
Point3D(double a, double b, double c):z(c){
setX(a);
setY(b);
}
double getZ() {
return this->z;
}
virtual double distance(Point2D & point2)
{
Point3D* rem = static_cast<Point3D*>(&point2);
double dz = getZ() - rem->z;
double dx = getX() - point2.getX();
double dy = getY() - point2.getY();
return sqrt(dz*dz + dx * dx + dy * dy);
}
};

#include <iostream>
using namespace std;

void printDistance(Point2D & point1, Point2D & point2) {
cout << "point1.distance(point2) = " <<
point1.distance(point2) << endl;
}

int main() {
double x1;
double y1;
double z1;
double x2;
double y2;
double z2;
cin >> x1;
cin >> y1;
cin >> z1;
cin >> x2;
cin >> y2;
cin >> z2;

Point3D point1(x1, y1, z1);
Point3D point2(x2, y2, z2);

cout << "point1.getX() = " << point1.getX() << endl;
cout << "point1.getY() = " << point1.getY() << endl;
cout << "point1.getZ() = " << point1.getZ() << endl;
cout << "point2.getX() = " << point2.getX() << endl;
cout << "point2.getY() = " << point2.getY() << endl;
cout << "point2.getZ() = " << point2.getZ() << endl;

printDistance(point1, point2);
}
1
2
3
4
5
6
7
8
9
10
11
EXAMPLE INPUT:
1 2 3
10 20 30
EXAMPLE OUTPUT:
point1.getX() = 1
point1.getY() = 2
point1.getZ() = 3
point2.getX() = 10
point2.getY() = 20
point2.getZ() = 30
point1.distance(point2) = 33.6749
Donate? comment?