reinterpret_cast和C风格的cast有什么区别? [英] What is the difference between a reinterpret_cast and a C-style cast?

查看:215
本文介绍了reinterpret_cast和C风格的cast有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两者之间的区别:

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

和:

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


推荐答案

C风格的Cast可以是以下任何类型的Cast:

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


  • const_cast

  • static_cast

  • static_cast 后跟一个 const_cast

  • reinterpret_cast

  • reinterpret_cast 后接 const_cast

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

从列表中可以做的第一个是C风格的转换将执行什么(从C ++ 03 5.4:显式类型转换(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)"

所以你的例子:

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

code> static_cast 。

the type of cast used would be a static_cast.

y = reinterpret_cast< int>(x); 将无法编译。

这篇关于reinterpret_cast和C风格的cast有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆