C++四種強制轉換

reinterpret_cast

/*  
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes.  
The operation result is a simple binary copy of the value from one pointer to the other.  
All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.  
It can also cast pointers to or from integer types.   
The format in which this integer value represents a pointer is platform-specific.   
The only guarantee is that a pointer cast to an integer type large enough to fully contain it (such as intptr_t),  
is guaranteed to be able to be cast back to a valid pointer.  
reinterpret可用於任何指針向任何指針的轉換。它只是簡單的進行二進制拷貝。  
它還可以用於將指針類型和整型類型相互轉換(注意整型類型和指針類型的長度不一致)。  
它不進行類型檢查。  
*/ 

reinterpret_cast轉換是在類C轉換的基礎上,在編譯期間
約束了整型、浮點型和枚舉類型的相互轉換。
static_cast

/*  
static_cast can perform conversions between pointers to related classes,   
not only upcasts (from pointer-to-derived to pointer-to-base),  
but also downcasts (from pointer-to-base to pointer-to-derived).   
No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type.   
Therefore, it is up to the programmer to ensure that the conversion is safe.   
On the other side, it does not incur the overhead of the type-safety checks of dynamic_cast.  
它用於在存在繼承關係的類指針之間轉換。可以從派生類指針轉爲基類指針,也可以從基類指針轉爲派生類指針。  

static_cast is also able to perform all conversions allowed implicitly  
(not only those with pointers to classes), and is also able to perform the opposite of these.   
It can:  
Convert from void* to any pointer type. In this case,   
it guarantees that if the void* value was obtained by converting from that same pointer type, the resulting pointer value is the same.  
Convert integers, floating-point values and enum types to enum types.  
它可以將void*型向任意指針類型轉換。還可以在整型、浮點型和枚舉型將相互轉換。  
*/ 

static_cast
約束了指針和整型的相互轉換。
約束了無關係類型的指針的相互轉換。(無類型指針除外)
dynamic_cast

/*  
dynamic_cast can only be used with pointers and references to classes (or with void*).   
Its purpose is to ensure that the result of the type conversion points to a valid complete object of the destination pointer type.  
This naturally includes pointer upcast (converting from pointer-to-derived to pointer-to-base), in the same way as allowed as an implicit conversion.  
But dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members)  
if -and only if- the pointed object is a valid complete object of the target type.  

dynamic_cast can also perform the other implicit casts allowed on pointers: casting null pointers between pointers types (even between unrelated classes),  
and casting any pointer of any type to a void* pointer.  
dynamic_cast只可以用於指針之間的轉換,它還可以將任何類型指針轉爲無類型指針,甚至可以在兩個無關係的類指針之間轉換。  
*/ 

(主要的功能是提供向下轉換時的安全檢測。子類轉換爲父類是安全的,反之則未必)
將一個基類對象指針(或引用)cast到繼承類指針,dynamic_cast會根據基類指針是否真正指向繼承類指針來做相應處理,若是轉換失敗,dynamic_cast將會對這次操作返回Null。
如果轉換目標是引用類型並且失敗了,dynamic_cast將拋出一個bad_cast異常

一般來說,因爲RTTI技術作用於運行時,所以其會產生運行時的代價。所以很多人建議,如果在能明確轉換安全的場景下,不要使用dynamic_cast方法進行轉換,而是使用static_cast,以免進行一些不必要的運行時計算。
const_cast
const_cast轉換符是用來移除變量的const或volatile限定符
我們可能調用了一個參數不是const的函數,而我們要傳進去的實際參數確實const的,但是我們知道這個函數是不會對參數做修改的。於是我們就需要使用const_cast去除const限定,以便函數能夠接受這個實際參數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章