当前位置:首页 > 通信资讯 > 正文

c++的四种类型转换(c需要类型转换)

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

  • static_cast
  • const_cast
  • dynamic_cast
  • reinterpret_cast

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

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

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

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

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

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

static_cast

使用方式:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <iostream> 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<int>(f); cout << "i " << i << endl; int *pi = static_cast<int *>(&f); // error invalid static_cast from type ‘float*' to type ‘int*' Derive d; d.Func(); Base *b = static_cast<Base *>(&d); b->Func(); return 0; }

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

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

dynamic_cast

使用方式:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> 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<Base *>(&d); b->Func(); Derive *dd = dynamic_cast<Derive *>(b); dd->Func(); return 0; }

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

const_cast

使用方式:

?
1 2 3 4 5 6 7 int main() { int data = 10; const int *cpi = &data; int *pi = const_cast<int *>(cpi); const int *cpii = const_cast<const int *>(pi); return 0; }

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

reinterpret_cast

使用方式:

?
1 2 3 4 5 6 int main() { int data = 10; int *pi = &data; float *fpi = reinterpret_cast<float *>(pi); return 0; }

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

到此这篇关于C++为什么非要引入那几种类型转换的文章就介绍到这了,更多相关C++ 类型转换内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://developer.51cto.com/art/202103/649521.htm

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。