【草稿】【Modern C++ #2】 C++ 轉型 casting 整理 (C-style cast, static_cast, dynamic_cast, reinterpret_cast, const_cast)

前言

此文章目前尚未整理完成,如需學習完整內容可參考隨附的 reference,或自行 google 搜尋
但因為作者要整理的筆記太多,如果想早點看到整理後的文章,可以下方留言「期待整理」之類的… 我會努力找時間優先整理!

casting 就是轉型的意思,這篇文章讓我們來整理一些 C, C++ 一些相關的概念

C-style cast (Regular Cast)

C 語言的轉型,格式是 (int) a,
透過 () 包住 type,
可以將後者變數轉型成 () 中的 type。

在 C++ 中,這種語法包含了 const_cast, static_cast, reinterpret_cast,不過也因此這種轉型不見得安全(下方有說明),因為他沒有採取比較安全的 dynamic_cast,可能會出現不預期的轉型(或說是語意混淆 ambiguous),因此使用時必須要小心。

缺點? 其實是因為從 C 到了 C++

C 語言能夠轉型的方式只有一種,但到了 C++ 後,出現了物件導向的「繼承」、「模版」概念
這在 C 上單純轉型的行為,可能開始會有語意混淆 (ambiguous),
而這時 compiler 的預設行為是「照著某個順序嘗試」,天啊,嘗試聽起來就可能突然哪天會出事啊!
當然,大部分情況可能沒差,但一個嚴謹的程式設計,
又特別是撰寫 C++ (相比 python),對這種小細節我們當然必須要求。

也因此後來才會出現了我們以下要介紹的 static_cast, dynamic_cast, reinterpret_cast, const_cast

介紹一下從 C++ 開始要處理的問題

如果我們把各種轉型的情況列出來,大致有以下幾種可能:

此部分參考原文翻譯,翻譯能力有限,建議英文不錯的也可以閱讀原文:

  1. How do you explain the differences among static_cast, reinterpret_cast, const_cast, and dynamic_cast to a new C++ programmer?
  2. Regular cast vs. static_cast vs. dynamic_cast in C++
  3. When should static_cast, dynamic_cast, const_cast, and reinterpret_cast be used?
  • 兩個可運算的 type
  • 把運算 type 轉成 pointer type
  • 兩個 pointer type 的轉換

C-style cast (Regular Cast) 範例程式碼

#include <iostream>

int main(){
    double a = 1.5;
    int b = (int) 1.5;
    std::cout << b << std::endl;
    std::cout << &a << std::endl;
    std::cout << (int) a << std::endl;
    std::cout << (float) a << std::endl;
    return 0;
}
  • 結果:

就是單純的轉型

static_cast

最基本的型態轉換,屬於強制轉型
常用於 float -> int,char -> int

dynamic_cast

dynamic_cast 最常被使用來處理多型 (polymorphism) 的問題,當我們要轉型至「被繼承的 class」才需要使用, dynamic_cast只被使用於 cast from “base class” to “derived class”.

reinterpret_cast

reinterpret_cast 是最危險的 casting,使用時最需要小心,他的強制轉型有可能會出現我們完全預期的結果。
使用於重新解讀比較 low level 的情況,(舉例像是 bit patterns),
最主要的使用情況是將 raw data bit stream 轉換為實際可以使用的資料,或將資料儲存為 low bits,並使用 pointer 指向這些資料。

const_cast

當我們要對一個 variable 新增/移除 const 時使用,
理解上,建議理解 「const type」 與 「type」 是不同的 type,
會比較不容易出現 casting 的問題。

Reference