12

C++为什么非要引入那几种类型转换?

 3 years ago
source link: http://developer.51cto.com/art/202012/637925.htm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

大家好,我是程序喵,今天我们放松下,看一些类型转换的知识点!

众所周知C++关于类型转换引入了四种方式:

static_cast

const_cast

dynamic_cast

reinterpret_cast

图片

为什么要引入这几种类型转换,它与C语言中的强制类型转换有什么区别?

这四种类型转换分别应用在什么场景?

C++为什么要引入这几种强制类型转换?

我们都知道C++完全兼容C语言,C语言的转换方式很简单,可以在任意类型之间转换,但这也恰恰是缺点,因为极其不安全,可能不经意间将指向const对象的指针转换成非const对象的指针,可能将基类对象指针转成了派生类对象的指针,这种转换很容易出bug,需要严格审查代码才能消除这种隐患,但是C这种转换方式不利于我们审查代码,且程序运行时也可能会出bug。

而C++引入的这几种类型转换可以完美的解决上述问题,不同场景下不同需求使用不同的类型转换方式,同时有利于代码审查。

下面详细介绍这四种类型转换的使用场景:

static_cast

使用方式:

#include

using namespace std;

struct Base {

virtual void Func() { cout << "Base Func \n"; }

};

struct Derive : public Base {

void Func() override { cout << "Derive Func \n"; }

};

int main() {

float f = 1.23;

cout << "f " << f << endl;

int i = static_cast (f);

cout << "i " << i << endl;

int *pi = static_cast (&f); // error invalid static_cast from type ‘float*’ to type ‘int*’

Derive d;

d.Func();

Base *b = static_cast(&d);

b->Func();

return 0;

}

使用场景:基本数据类型之间的转换使用,例如float转int,int转char等,在有类型指针和void*之间转换使用,子类对象指针转换成父类对象指针也可以使用static_cast。

非多态类型转换一般都使用static_cast,而且最好把所有的隐式类型转换都是用static_cast进行显示替换,不能使用static_cast在有类型指针之间进行类型转换。

dynamic_cast

使用方式:

#include

using namespace std;

struct Base {

virtual void Func() { cout << "Base Func \n"; }

};

struct Derive : public Base {

void Func() override { cout << "Derive Func \n"; }

};

int main() {

Derive d;

d.Func();

Base *b = dynamic_cast(&d);

b->Func();

Derive *dd = dynamic_cast (b);

dd->Func();

return 0;

}

使用场景:用于将父类的指针或引用转换为子类的指针或引用,此场景下父类必须要有虚函数,因为dynamic_cast是运行时检查,检查需要运行时信息RTTI,而RTTI存储在虚函数表中,关于虚函数表具体可以看我的这篇文章:面试系列之C++的对象布局

const_cast

使用方式:

int main() {

int data = 10;

const int *cpi = &data;

int *pi = const_cast (cpi);

const int *cpii = const_cast (pi);

return 0;

}

使用场景:用于常量指针或引用与非常量指针或引用之间的转换,只有const_cast才可以对常量进行操作,一般都是用它来去除常量性,去除常量性是危险操作,还是要谨慎操作。

reinterpret_cast

使用方式:

int main() {

int data = 10;

int *pi = &data;

float *fpi = reinterpret_cast (pi);

return 0;

}

使用场景:没啥场景,类似C语言中的强制类型转换,什么都可以转,万不得已不要使用,一般前三种转换方式不能解决问题了使用这种强制类型转换方式。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK