什么时候应该使用static_cast,dynamic_cast,const_cast和reinterpret_cast? [英] When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

查看:143
本文介绍了什么时候应该使用static_cast,dynamic_cast,const_cast和reinterpret_cast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下各项的正确用途:


  • static_cast

  • dynamic_cast

  • const_cast

  • reinterpret_cast

  • C风格演员 $ c>

  • 函数式转换类型(值)

  • static_cast
  • dynamic_cast
  • const_cast
  • reinterpret_cast
  • C-style cast (type)value
  • Function-style cast type(value)

如何决定在哪些特定情况下使用哪一个?

How does one decide which to use in which specific cases?

推荐答案

static_cast 是您应该尝试使用的第一个投射。它做类似类型之间的隐式转换(例如 int float 或指向 void * ),它也可以调用显式转换函数(或隐式转换函数)。在许多情况下,明确说明 static_cast 是没有必要的,但重要的是注意 T(something)语法相当于(T)something ,应该避免(稍后再说)。 $ T(something,something_else)是安全的,并且保证调用构造函数。

static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions (or implicit ones). In many cases, explicitly stating static_cast isn't necessary, but it's important to note that the T(something) syntax is equivalent to (T)something and should be avoided (more on that later). A T(something, something_else) is safe, however, and guaranteed to call the constructor.

static_cast 也可以通过继承层次结构进行转换。当向上(向基类)投射时,不必要,但向下投射时,只要不通过 virtual 继承,就可以使用它。然而,它不做检查,它是未定义的行为 static_cast 下一个层次结构类型,实际上不是对象的类型。

static_cast can also cast through inheritance hierarchies. It is unnecessary when casting upwards (towards a base class), but when casting downwards it can be used as long as it doesn't cast through virtual inheritance. It does not do checking, however, and it is undefined behavior to static_cast down a hierarchy to a type that isn't actually the type of the object.

const_cast 可用于删除 const 到变量;没有其他C ++转换能够删除它(甚至 reinterpret_cast )。重要的是要注意,如果原来的变量是 const ,修改以前的 const 如果你使用它来取消不是使用 const 声明的对象的引用,那么它是安全的( const 。例如,当基于 const 重载成员函数时,这可能很有用。它还可以用于向对象添加 const ,例如调用成员函数重载。

const_cast can be used to remove or add const to a variable; no other C++ cast is capable of removing it (not even reinterpret_cast). It is important to note that modifying a formerly const value is only undefined if the original variable is const; if you use it to take the const off a reference to something that wasn't declared with const, it is safe. This can be useful when overloading member functions based on const, for instance. It can also be used to add const to an object, such as to call a member function overload.

code> const_cast volatile 上也可以工作。

const_cast also works similarly on volatile, though that's less common.

dynamic_cast 几乎专门用于处理多态性。您可以将任何多态类型的指针或引用转换为任何其他类类型(多形类型至少有一个虚函数,声明或继承)。你可以使用它不仅仅是向下投 - 你可以横向或甚至另一个链。 dynamic_cast 将寻找所需的对象并返回,如果可能的话。如果不能,在指针的情况下,它将返回 nullptr ,或者将 std :: bad_cast 引用的情况。

dynamic_cast is almost exclusively used for handling polymorphism. You can cast a pointer or reference to any polymorphic type to any other class type (a polymorphic type has at least one virtual function, declared or inherited). You can use it for more than just casting downwards -- you can cast sideways or even up another chain. The dynamic_cast will seek out the desired object and return it if possible. If it can't, it will return nullptr in the case of a pointer, or throw std::bad_cast in the case of a reference.

dynamic_cast 虽然有一些限制。如果在继承层次结构中存在多个相同类型的对象(所谓的可怕的钻石),并且不使用 virtual 继承,则它不起作用。它也只能通过公共继承 - 它总是不能通过 protected private 继承。这很少是一个问题,因为这种形式的继承是罕见的。

dynamic_cast has some limitations, though. It doesn't work if there are multiple objects of the same type in the inheritance hierarchy (the so-called 'dreaded diamond') and you aren't using virtual inheritance. It also can only go through public inheritance - it will always fail to travel through protected or private inheritance. This is rarely an issue, however, as such forms of inheritance are rare.

reinterpret_cast 是最危险的投射,应该非常谨慎地使用。它将一种类型直接转换为另一种类型 - 例如将值从一个指针转换为另一个指针,或者在 int 中存储指针,或者其他所有类型的令人讨厌的东西。大部分情况下,使用 reinterpret_cast 获得的唯一保证是,通常如果将结果转换回原始类型,您将获得完全相同的值(但 ,如果中间类型小于原始类型)。有很多转换 reinterpret_cast 也无法做到。它主要用于特别奇怪的转换和位操作,例如将原始数据流转换为实际数据,或将数据存储在对齐指针的低位。

reinterpret_cast is the most dangerous cast, and should be used very sparingly. It turns one type directly into another - such as casting the value from one pointer to another, or storing a pointer in an int, or all sorts of other nasty things. Largely, the only guarantee you get with reinterpret_cast is that normally if you cast the result back to the original type, you will get the exact same value (but not if the intermediate type is smaller than the original type). There are a number of conversions that reinterpret_cast cannot do, too. It's used primarily for particularly weird conversions and bit manipulations, like turning a raw data stream into actual data, or storing data in the low bits of an aligned pointer.

C cast 是使用(类型)对象 / code>。 C风格的Cast定义为以下的第一个成功:

C casts are casts using (type)object or type(object). A C-style cast is defined as the first of the following which succeeds:


  • const_cast

  • static_cast (虽然忽略存取限制)

  • static_cast (见上文),然后 const_cast

  • reinterpret_cast

  • reinterpret_cast ,然后 const_cast

  • const_cast
  • static_cast (though ignoring access restrictions)
  • static_cast (see above), then const_cast
  • reinterpret_cast
  • reinterpret_cast, then const_cast

因此,在某些情况下,它可以用来代替其他类型的转换,但是由于能够转换为 reinterpret_cast ,除非你确定 static_cast 会成功或 reinterpret_cast 将失败。即使是这样,考虑更长,更明确的选项。

It can therefore be used as a replacement for other casts in some instances, but can be extremely dangerous because of the ability to devolve into a reinterpret_cast, and the latter should be preferred when explicit casting is needed, unless you are sure static_cast will succeed or reinterpret_cast will fail. Even then, consider the longer, more explicit option.

C风格的转换也会忽略执行 static_cast ,这意味着他们有能力执行一个操作,没有其他转换可以。这个主要是一个kludge,虽然,在我看来,只是另一个原因,以避免C风格的演员。

C-style casts also ignore access control when performing a static_cast, which means that they have the ability to perform an operation that no other cast can. This is mostly a kludge, though, and in my mind is just another reason to avoid C-style casts.

这篇关于什么时候应该使用static_cast,dynamic_cast,const_cast和reinterpret_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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