static_cast<> 和 C 风格的转换有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1609163/
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-27 20:36:01  来源:igfitidea点击:

What is the difference between static_cast<> and C style casting?

c++castingstatic-cast

提问by dicroce

Is there any reason to prefer static_cast<>over C style casting? Are they equivalent? Is their any sort of speed difference?

有什么理由更喜欢static_cast<>C 风格的铸造吗?它们是等价的吗?他们有什么速度差异吗?

回答by Glen

C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime

C++ 样式转换由编译器检查。C 样式转换不是并且可能在运行时失败

also, c++ style casts can be searched for easily, whereas it's really hard to search for c style casts

此外,可以轻松搜索 c++ 样式转换,而搜索 c 样式转换非常困难

Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly.

另一个很大的好处是 4 种不同的 C++ 风格强制转换更清楚地表达了程序员的意图。

When writing C++ I'd pretty much always use the C++ ones over the the C style.

在编写 C++ 时,我几乎总是使用 C++ 而不是 C 风格。

回答by Rika

In short:

简而言之

  1. static_cast<>()gives you a compile time checking ability, C-Style cast doesn't.
  2. static_cast<>()is more readable and can be spotted easily anywhere inside a C++ source code, C_Style cast is'nt.
  3. Intentions are conveyed much better using C++ casts.
  1. static_cast<>()给你一个编译时检查能力,C-Style cast 没有。
  2. static_cast<>()更具可读性并且可以很容易地在 C++ 源代码中的任何地方发现,C_Style cast is'nt。
  3. 使用 C++ 强制转换可以更好地传达意图。

More Explanation:

更多解释

The static cast performs conversions between compatible types. It is similar to the C-style cast, but is more restrictive. For example, the C-style cast would allow an integer pointer to point to a char.

静态类型转换执行兼容类型之间的转换。它类似于 C 风格的强制转换,但限制性更强。例如,C 风格的强制转换将允许一个整数指针指向一个字符。

char c = 10;       // 1 byte
int *p = (int*)&c; // 4 bytes

Since this results in a 4-byte pointer ( a pointer to 4-byte datatype) pointing to 1 byte of allocated memory, writing to this pointer will either cause a run-time error or will overwrite some adjacent memory.

由于这会导致 4 字节指针(指向 4 字节数据类型的指针)指向已分配内存的 1 字节,因此写入此指针将导致运行时错误或覆盖某些相邻内存。

*p = 5; // run-time error: stack corruption

In contrast to the C-style cast, the static cast will allow the compiler to check that the pointer and pointee data types are compatible, which allows the programmer to catch this incorrect pointer assignment during compilation.

与 C 风格的转换不同,静态转换将允许编译器检查指针和被指点数据类型是否兼容,这允许程序员在编译期间捕获这种不正确的指针分配。

int *q = static_cast<int*>(&c); // compile-time error

You can also check this page on more explanation on C++ casts : Click Here

您还可以查看此页面上有关 C++ 强制转换的更多说明:单击此处

回答by Eugene Yokota

See A comparison of the C++ casting operators.

请参阅C++ 强制转换运算符的比较

However, using the same syntax for a variety of different casting operations can make the intent of the programmer unclear.

Furthermore, it can be difficult to find a specific type of cast in a large codebase.

the generality of the C-style cast can be overkill for situations where all that is needed is a simple conversion. The ability to select between several different casting operators of differing degrees of power can prevent programmers from inadvertently casting to an incorrect type.

但是,对各种不同的强制转换操作使用相同的语法会使程序员的意图变得不清楚。

此外,很难在大型代码库中找到特定类型的演员表。

对于只需要简单转换的情况,C 样式转换的通用性可能会过大。在不同权力程度的几个不同强制转换运算符之间进行选择的能力可以防止程序员无意中强制转换为不正确的类型。

回答by Rishi Khaneja

struct A {};
struct B : A {};
struct C {}; 

int main()
{
    A* a = new A;    

    int i = 10;

    a = (A*) (&i); // NO ERROR! FAIL!

    //a = static_cast<A*>(&i); ERROR! SMART!

    A* b = new B;

    B* b2 = static_cast<B*>(b); // NO ERROR! SMART!

    C* c = (C*)(b); // NO ERROR! FAIL!

    //C* c = static_cast<C*>(b); ERROR! SMART!
}

回答by Ying Xiong

A great post explaining different casts in C/C++, and what C-style cast really does: https://anteru.net/blog/2007/12/18/200/index.html

一篇很好的文章,解释了 C/C++ 中的不同类型转换,以及 C 风格类型转换的真正作用:https: //anteru.net/blog/2007/12/18/200/index.html

C-Style casting, using the (type)variable syntax. The worst ever invented. This tries to do the following casts, in this order: (see also C++ Standard, 5.4 expr.cast paragraph 5)

  1. const_cast
  2. static_cast
  3. static_cast followed by const_cast
  4. reinterpret_cast
  5. reinterpret_castfollowed by const_cast

C 样式转换,使用(类型)变量语法。有史以来最糟糕的发明。这将尝试按以下顺序执行以下强制转换:(另请参阅 C++ 标准,5.4 expr.cast 第 5 段)

  1. const_cast
  2. static_cast
  3. static_cast 后跟 const_cast
  4. reinterpret_cast
  5. reinterpret_castfollowed by const_cast

回答by kiriloff

static_castchecks at compile time that conversion is not between obviously incompatible types. Contrary to dynamic_cast, no check for types compatibility is done at run time. Also, static_castconversion is not necessarily safe.

static_cast在编译时检查转换不在明显不兼容的类型之间。与 相反dynamic_cast,在运行时不检查类型兼容性。此外,static_cast转换不一定是安全的。

static_castis used to convert from pointer to base class to pointer to derived class, or between native types, such as enum to int or float to int.

static_cast用于从指向基类的指针到指向派生类的指针,或在本机类型之间转换,例如 enum 到 int 或 float 到 int。

The user of static_castmust make sure that the conversion is safe.

的用户static_cast必须确保转换是安全的。

The C-style cast does not perform any check, either at compile or at run time.

无论是在编译时还是在运行时,C 风格的强制转换都不执行任何检查。

回答by Doug T.

Since there are many different kinds of casting each with different semantics, static_cast<> allows you to say "I'm doing a legal conversion from one type to another" like from int to double. A plain C-style cast can mean a lot of things. Are you up/down casting? Are you reinterpreting a pointer?

由于有许多不同类型的强制转换,每种类型都有不同的语义,因此 static_cast<> 允许您说“我正在进行从一种类型到另一种类型的合法转换”,例如从 int 到 double。一个简单的 C 风格类型转换可能意味着很多事情。你是向上/向下铸造吗?你在重新解释一个指针吗?