C++ static_cast 和 reinterpret_cast 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6855686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:46:47  来源:igfitidea点击:

What is the difference between static_cast and reinterpret_cast?

c++ccastingstruct

提问by HariHaraSudhan

Possible Duplicate:
When should static_cast, dynamic_cast and reinterpret_cast be used?

可能的重复:
什么时候应该使用 static_cast、dynamic_cast 和 reinterpret_cast?

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++ 中使用 c 函数,其中在 c 中作为 void 类型参数传递的结构直接存储相同的结构类型。

eg in C.

例如在 C 中。

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

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

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

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

and when i use reinterpret_castit does the same job, casting the struct

当我使用 reinterpret_cast它做同样的工作时,转换结构

when i use Testitem *it=(Testitem *)data;

当我使用 Testitem *it=(Testitem *)data;

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

这也做同样的事情。但是如何使用它们三个来影响结构。

回答by templatetypedef

A static_castis 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_casta void*to an int*, since the void*might actually point at an int*, or an intto a char, since such a conversion is meaningful. However, you cannot static_castan int*to a double*, since this conversion only makes sense if the int*has somehow been mangled to point at a double*.

Astatic_cast是从一种类型到另一种类型的转换,(直觉上)是在某些情况下可以成功并在没有危险转换的情况下有意义的转换。例如,您可以static_castavoid*到 an int*,因为void*实际上可能指向 anint*或 anint到 a char,因为这样的转换是有意义的。但是,您不能将static_castanint*指向 a double*,因为这种转换仅在int*以某种方式被损坏为指向 a时才有意义double*

A reinterpret_castis 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 intto a void*is perfectly legal with reinterpret_cast, though it's unsafe.

Areinterpret_cast是表示不安全转换的强制转换,该转换可能将一个值的位重新解释为另一个值的位。例如,int*将 a强制转换为 a 对 adouble*是合法的reinterpret_cast,尽管结果未指定。类似地,将 an 转换int为 avoid*是完全合法的reinterpret_cast,尽管它是不安全的。

Neither static_castnor reinterpret_castcan remove constfrom something. You cannot cast a const int*to an int*using either of these casts. For this, you would use a const_cast.

static_cast不能也不能从某物中reinterpret_cast移除const。您不能使用这些强制转换中的任何const int*一个将 aint*强制转换为 an 。为此,您将使用const_cast.

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

这种形式的 C 风格强制转换(T)被定义为尽可能尝试执行 a static_castreinterpret_cast如果不起作用则退回到 a 。const_cast如果绝对必须,它也将适用。

In general, you should always prefer static_castfor 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_castif 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应该安全的演员表。如果您不小心尝试执行未明确定义的强制转换,则编译器将报告错误。仅reinterpret_cast当您所做的确实改变了机器中某些位的解释时才使用,并且仅当您愿意冒险执行reinterpret_cast. 对于您的情况,您应该使用static_cast,因为void*在某些情况下从 的向下转换是明确定义的。