reinterpret_cast<>()诉.static_cast<>() [英] reinterpret_cast<>() v. static_cast<>()

查看:73
本文介绍了reinterpret_cast<>()诉.static_cast<>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我检查了几个在线资源,我无法确定如何使用

reinterpret_cast<>与static_cast<>不同。他们似乎都在
执行一种类型的编译时转换。但是,我肯定

还有其他事情正在发生。


有人可以解释差异或推荐一个可以
向我解释一下?


谢谢,

Scott


-

删除.nospam从我的电子邮件中的用户ID通过电子邮件回复。

Hi, everyone,

I''ve checked a couple of on-line resources and am unable to determine how
reinterpret_cast<> is different from static_cast<>. They both seem to
perform a compile-time casting of one type to another. However, I''m certain
that there is something else that is happening.

Can someone explain the difference or recommend an online site that can
explain it to me?

Thanks,
Scott

--
Remove ".nospam" from the user ID in my e-mail to reply via e-mail.

推荐答案

2004年1月13日星期二13:53:48 -0800在comp.lang.c ++中,Scott Brady

Drummonds < SC ********************** @ intel.com>据称写了

On Tue, 13 Jan 2004 13:53:48 -0800 in comp.lang.c++, "Scott Brady
Drummonds" <sc**********************@intel.com> was alleged to have
written:
大家,

我检查了几个在线资源,我无法确定
reinterpret_cast<>与static_cast<>不同。他们似乎都在执行一种类型的编译时转换。但是,我确定还有其他事情正在发生。
Hi, everyone,

I''ve checked a couple of on-line resources and am unable to determine how
reinterpret_cast<> is different from static_cast<>. They both seem to
perform a compile-time casting of one type to another. However, I''m certain
that there is something else that is happening.




static_cast<>使用明智,明确的转换。例如,

整数10将转换为双10.0。


reinterpret_cast<>只是看一下内存中的位,好像它们是一个不同类型的编码。例如,整数10将

看起来像某个数字谁知道什么是双。



static_cast<> uses sensible, well-defined conversions. For example, the
integer 10 would be converted to the double 10.0.

reinterpret_cast<> just looks at the bits in memory in as if they were
the encoding as a different type. For example, the integer 10 would
look like some number who-knows-what as a double.




Scott Brady Drummonds < SC ********************** @ intel.com>在消息新闻中写道:bu ********** @ news01.intel.com ...

"Scott Brady Drummonds" <sc**********************@intel.com> wrote in message news:bu**********@news01.intel.com...
大家好,

我检查了一下几个在线资源,我无法确定如何重新解释<>与static_cast<>不同。他们似乎都在执行一种类型的编译时转换。但是,我确定还有其他事情正在发生。
Hi, everyone,

I''ve checked a couple of on-line resources and am unable to determine how
reinterpret_cast<> is different from static_cast<>. They both seem to
perform a compile-time casting of one type to another. However, I''m certain
that there is something else that is happening.




有两个主要区别:

1. static_cast不会转换某些类型(你会得到一个编译错误)。

2.当两者都执行转换时,转换的性质是不同的。


Static_cast有两个用途。首先它会强制进行可能发生隐式的转换。

例如:

double value = 1.45;

double remaining = value - static_cast< ; int>(value);


这里我们强制转换可以用一个额外的临时变量隐式完成。

另一个用途,就是反转定义的转换。如果有有效的正向转换,

在大多数情况下你可以通过静态转换来反转它。


struct Base {

};

struct派生:基础{

};


派生d;

基础* bp =& d; //隐式转换。

派生* dp = static_cast< Derived *>(bp); //反转有效转换。


重新解释演员说,将这些位视为另一种类型。其结果是在大多数情况下具体实施

。你可以这样做:

char * foo = ...

unsigned char * foo = reinterpret_cast< unsigned char *>(foo); //强制改变类型!


不用说在做这个之前你应该三思而后行。编译器将

拒绝执行上述类型的static_cast,因为它们是无关的(没有转换

定义在任一方向)。


现在这里是一个案例,他们两个演员都会做一些不同的事情(在很多情况下):


struct BaseA {

char a; < br $>
};

struct BaseB {

char b;

};

struct派生:BaseA,BaseB {

};


派生d;

BaseB * bp = static_cast< BaseB *>( &安培; d); //有效:bp现在指向BaseB子对象d

BaseB * rp = reinterpret_cast< BaseB *>(& d); //可能无效,可能导致rp指向

// d中的BaseA内存但是使用BaseB *类型。



There are two major differences:
1. static_cast won''t convert certain types (you''ll get a compile error).
2. when both will perform the conversion, the nature of the conversion is different.

Static_cast has two uses. First it will force a conversion that could have happened implicitly.
For example:
double value = 1.45;
double remainder = value - static_cast<int>(value);

Here we force a conversion that could be done implicitly with an extra temporary variable.
The other use, is to reverse a defined conversion. If there is a valid forward conversion,
in most cases you can reverse it with a static cast.

struct Base {
};
struct Derived : Base {
};

Derived d;
Base* bp = &d; // implicit conversion.
Derived* dp = static_cast<Derived*>(bp); // invert valid conversion.

A reinterpret cast pretty much says, treat these bits as another type. Its results are implementation
specific in most cases. You can do things like:
char* foo = ...

unsigned char* foo = reinterpret_cast<unsigned char*>(foo); // force type change!

Needless to say you should think twice in general before doing this. The compiler will
refuse to do the static_cast of the above types because they are unrelated (no conversion
defined in either direction).

Now here''s a case where they both casts will do something different (in many circumstances):

struct BaseA {
char a;
};
struct BaseB {
char b;
};
struct Derived : BaseA, BaseB {
};

Derived d;
BaseB* bp = static_cast<BaseB*>(&d); // valid: bp now points to BaseB subobject of d
BaseB* rp = reinterpret_cast<BaseB*>(&d); // probably not valid, could result in rp pointing at
// the BaseA memory in d but with the BaseB* type.




" Scott Brady Drummonds" < SC ********************** @ intel.com>写在

消息新闻:bu ********** @ news01.intel.com ...

"Scott Brady Drummonds" <sc**********************@intel.com> wrote in
message news:bu**********@news01.intel.com...
有人可以解释差异或推荐在线可以向我解释的网站吗?
Can someone explain the difference or recommend an online site that can
explain it to me?




static_cast<>适用于相关演员; (float-> int,int-> enum等)。

reinterpret_cast<>用于完全不相关的类型转换(通常不应该执行
)。例如:


//将十六进制数转换为设备端口

IO_device * myDevicePort = reinterpret_cast< IO_device *>(0XFF00)//设备位于

0xFF00



static_cast<> is for related casts; (float->int, int->enum, etc).
reinterpret_cast<> is for totally unrelated type conversions (which
shouldn''t usually be done). For example:

// Converting a hex number to a device port
IO_device* myDevicePort = reinterpret_cast<IO_device*>(0XFF00) // device at
0xFF00


这篇关于reinterpret_cast&lt;&gt;()诉.static_cast&lt;&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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