static_cast dynamic_cast reinterpret_cast 使用准则 guide from stackoverflow

static_castis thefirstcast 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 statingstatic_castisn't necessary, but it's important to note that theT(something)syntax is equivalent to(T)somethingand should be avoided (more on that later). AT(something, something_else)is safe, however, and guaranteed to call the constructor.

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

const_castcan be used to remove or addconstto a variable; no other C++ cast is capable of removing it (not evenreinterpret_cast). It is important to note that using it is only undefined if the orginial variable isconst; if you use it to take theconstof a reference to something that wasn't declared withconst, it is safe. This can be useful when overloading member functions based onconst, for instance. It can also be used to addconstto an object, such as to call a member function overload.

const_castalso works similarly onvolatile, though that's less common .

dynamic_castis almostexclusively 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 don't have to use it to cast downwards, you can cast sideways or even up another chain. Thedynamic_castwill seek out the desired object and return it if possible. If it can't, it will returnNULLin the case of a pointer, or throwstd::bad_castin the case of a reference.

dynamic_casthas 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 usingvirtualinheritance. It also can only go through public inheritance - it will always fail to travel throughprotectedorprivateinheritance.This is rarely an issue, however, as such forms of inheritance are rare.


reinterpret_castis themost dangerouscast, 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 anint, or all sorts of other nasty things. Largely, the only guarantee you get withreinterpret_castis that if you cast the result back to the original type, you will get the same value. Other than that, you're on your own.reinterpret_castcannot do all sorts of conversions; in fact it is relatively limited. It should almost never be used (even interfacing with C code usingvoid*can be done withstatic_cast).


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

  • const_cast
  • static_cast
  • static_cast, thenconst_cast
  • reinterpret_cast
  • reinterpret_cast, thenconst_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 areinterpret_cast, and the latter should be preferred when explicit casting is needed, unless you are surestatic_castwill succeed orreinterpret_castwill fail. Even then, consider the longer, more explicit option.

C-style casts also ignore access control when performing astatic_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.

I hope this helps!
原文链接: https://www.cnblogs.com/titer1/archive/2012/03/08/2385904.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/43736

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午8:21
下一篇 2023年2月8日 下午8:23

相关推荐