什么是static_cast并reinter pret_cast之间的区别? [英] What is the difference between static_cast and reinterpret_cast?

查看:191
本文介绍了什么是static_cast并reinter pret_cast之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinter$p$pt-cast-be-used\">When应的static_cast,将dynamic_cast,reinter使用pret_cast?

我使用C函数在C ++中,其中在C void类型参数传递的结构被直接存储相同的结构类型。

I'm using c function in c++, where a structure passed as a void type argument in c is directly stored that same structure type.

例如用C

void getdata(void *data){
    Testitem *ti=data;//Testitem is of struct type.
}

做同样的在C ++中,我使用的static_cast:

to do the same in c++ i use static_cast:

void foo::getdata(void *data){
    Testitem *ti = static_cast<Testitem*>(data);
}

和当我使用 reinter pret_cast 它做同样的工作,铸造结构

and when i use reinterpret_cast it does the same job, casting the struct

文我使用 Testitem *它=(Testitem *)的数据;

这也做了同样的thingg。
但是如何结构得到通过他们三个人的影响。

this also does the same thingg. But how is the structure gets affected by using the three of them.

推荐答案

A 的static_cast 是从一种类型强制转换为另一种(直觉)是一个演员,其中可以根据某些情况下取得成功,并在没有危险的投有意义。例如,您可以的static_cast A 无效* 为int * ,因为无效* 可能实际上指向一个为int * INT 字符,因为这种转换是有意义的。但是,你不能的static_cast 为int * 双* ,因为这种转换才有意义,如果在为int * 已莫名其妙地被错位在双* 。

A static_cast is a cast from one type to another that (intuitively) is a cast that could under some circumstance succeed and be meaningful in the absence of a dangerous cast. For example, you can static_cast a void* to an int*, since the void* might actually point at an int*, or an int to a char, since such a conversion is meaningful. However, you cannot static_cast an int* to a double*, since this conversion only makes sense if the int* has somehow been mangled to point at a double*.

A reinter pret_cast 是一个演员,其中重presents可能reinter preT一个值的位作为位不安全的转换另一个值。例如,投为int * 双* 是合法使用 reinter pret_cast ,虽然结果是不确定的。同样,投 INT 无效* 完全合法reinter pret_cast ,虽然这是不安全的。

A reinterpret_cast is a cast that represents an unsafe conversion that might reinterpret the bits of one value as the bits of another value. For example, casting an int* to a double* is legal with a reinterpret_cast, though the result is unspecified. Similarly, casting an int to a void* is perfectly legal with reinterpret_cast, though it's unsafe.

无论是的static_cast 也不 reinter pret_cast 可以删除常量的东西。你不能投了 const int的* 来使用这两种类型转换的为int * 。为此,你可以使用一个的const_cast

Neither static_cast nor reinterpret_cast can remove const from something. You cannot cast a const int* to an int* using either of these casts. For this, you would use a const_cast.

表格(T)的C样式转换被定义为试图在的static_cast 如果可以做到,退回到一个 reinter pret_cast 如果不工作。它也将采用的const_cast 如果它绝对必须。

A C-style cast of the form (T) is defined as trying to do a static_cast if possible, falling back on a reinterpret_cast if that doesn't work. It also will apply a const_cast if it absolutely must.

在一般情况下,你应该总是preFER 的static_cast 的类型转换,应该是安全的。如果你不小心尝试做的是不明确的,一个演员,那么编译器会报告错误。仅使用 reinter pret_cast 如果你正在做的真的是改变设备的某些位间pretation,只有使用C样式转换如果你愿意冒险做一个 reinter pret_cast 。对于你的情况,你应该使用的static_cast ,因为从低垂的无效* 在某些情况下是明确定义

In general, you should always prefer static_cast for casts that should be safe. If you accidentally try doing a cast that isn't well-defined, then the compiler will report an error. Only use reinterpret_cast if what you're doing really is changing the interpretation of some bits in the machine, and only use a C-style cast if you're willing to risk doing a reinterpret_cast. For your case, you should use the static_cast, since the downcast from the void* is well-defined in some circumstances.

这篇关于什么是static_cast并reinter pret_cast之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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