網上的文章但凡有提到 static_cast、const_cast、reinterpret_cast、dynamic_cast 的,都會語重心長的說,他們克服了 C 風格的類型轉換的缺點,應當使用它們。
可是,C 風格的到底有什么壞處?C++的這些 cast 又有什么好處呢?
昨天以前,我連這些 cast 是什么都不知道(很慚愧)。昨天因為同事們提到這件事,于是小小研究了一下。一些實驗代碼如下:
1、無繼承的類型轉換
class A { }; class B { public: operator A() { return A(); } }; int main() { B b; A a = (A)b; // 執行 operator A() A a2 = static_cast<A>(b); // 執行 operator A() A a3 = dynamic_cast<A>(b); // 不允許 A a4 = reinterpret_cast<A>(b); // 不允許 A a5 = const_cast<A>(b); // 不允許 return 0; } |
2、const_cast
struct A { int m; A(int m = 0) : m(m) { } }; int main() { const A a; A a2 = (A)a; // 允許,(A) 有沒有都一樣,a2 是個新變量 a2.m = 1; // a2 的改變不影響 a A &a3 = (A &)a; // 允許 a3.m = 2; // 影響 a // A &a4 = a; // 不允許,const 限定起作用了 A *pa5 = (A *)&a; // 允許 pa5->m = 3; // 影響 a // A *pa6 = &a; // 不允許,const 限定起作用了 // A aa2 = const_cast<A>(a); // 不允許 A &aa3 = const_cast<A &>(a); // 允許 aa3.m = 2; // 影響 a A *paa5 = const_cast<A *>(&a); // 允許 paa5->m = 3; // 影響 a const int i = 0; const int &i2 = i; const int *pi3 = &i; // int j = const_cast<int>(i); // 不允許 int &j2 = const_cast<int &>(i2); // 允許 int *pj3 = const_cast<int *>(pi3); // 允許 return 0; } |
從第1點的試驗,加上外界資料的說明,看上去const_case 只允許具有不同cv限定符的同類型之間的轉換。
值得注意的是,如果類型A不是指針或引用,不能使用const_cast(使用了也無意義,見 A a2 = (A)a 這一行)
在 const_cast 可以使用的情形,(T)value 形式都可以使用,(T)value 在功能上完全覆蓋 const_cast。
2、reinterpret_cast
class A { public: operator int *() { return nullptr; } }; int main() { int i = 0; double d = 1.0; int *p = nullptr; // int di = reinterpret_cast<int>(d); // 不允許 int pi = reinterpret_cast<int>(p); // 允許 // int pi2 = static_cast<int>(p); // 不允許 // double id = reinterpret_cast<double>(i);// 不允許 // double pd = reinterpret_cast<double>(p);// 不允許 int *ip = reinterpret_cast<int *>(i); // 允許 // int *ip2 = static_cast<int *>(i); // 不允許 // int *dp = reinterpret_cast<int *>(d); // 不允許 A a; int *pa = (int *)a; // 允許 int *pa2 = static_cast<int *>(a); // 允許 // int *p2 = reinterpret_cast<int *>(a); // 不允許 return 0; } |
看上去,reinterpret_cast 可以理解為在指針和數值之間轉換的一種方式,無關任何運算符重載,僅僅把指針轉為字面值,或者把數字轉為指針,轉換的過程中值沒有任何改變,只是告訴編譯器不要報類型不匹配而已。
另外,在reinterpret_cast可以使用的情形,static_cast 是不可以使用的,除非定義了相應的類型轉換運算符。
在 reinterpret_cast 可以使用的情形,(T)value 的方式同樣可以完全勝任,(T)value 在功能上完全覆蓋 reinterpret_cast。
dynamic_cast 我自認為還是理解的,就不試了。
綜上,我的理解如下:
1、static_cast + const_cast + reinterpret_cast = (T)value
C++ 把原來C風格的的這三個cast拆分成了三個,三者相互正交。大多數情況下,應該是 static_cast 在取代著 (T)value;只是在去除 cv 限定符的時候,換用 const_cast;在取指針字面值的時候,換用 reinterpret_cast。類型轉換運算符 operator T() 由 static_cast 負責執行。
2、dynamic_cast 是 C++ 新增的,用于多態的情形,且只允許轉換具有多態關系的繼承樹上的類型的指針和引用,不允許轉換類型本身。它不是針對 (T)value而出現的,兩者沒有任何競爭關系,只是取決于不同的需求。
(不知這樣理解是否正確,請批評指正~)
至于網上推崇用新寫法,是不是為了更細化而容易理解?有沒有什么是 (T)value 做不到而 *_cast 能做到的?或者反過來?
posted on 2012-03-23 09:53
溪流 閱讀(1751)
評論(0) 編輯 收藏 引用 所屬分類:
C++