What is the difference between a reinterpret_cast and a C-style cast? [duplicate]

放肆的年华 提交于 2019-12-13 04:24:49

问题


What is the difference between :

double x = 10.3;
int y;
y = (int) x;    // c-like cast notation

And :

double x = 10.3;
int y;
y = reinterpret_cast<int>(x)   

回答1:


A C-style cast can be any of the following types of casts:

  • const_cast
  • static_cast
  • static_cast followed by a const_cast
  • reinterpret_cast
  • reinterpret_cast followed by a const_cast

the first one from that list that can be done is the what the C-style cast will perform (from C++03 5.4: "Explicit type conversion (cast notation)"

So for your example:

double x = 10.3;
int y;
y = (int) x;

the type of cast used would be a static_cast.

And y = reinterpret_cast<int>(x); won't compile.



来源:https://stackoverflow.com/questions/22999529/what-is-the-difference-between-a-reinterpret-cast-and-a-c-style-cast

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!