skyer

|
分享:
▲
▼
1. C-Style 轉型.. 就是硬轉,管 Class 有沒有關係 在 Compile & Runtime 不作任何檢查.. 一但出錯就是....當! 在這裡,這個轉型是合法的.. 子類別可以轉型成父類別
2. 感覺怪怪的.. Car* pCar = static_cast <Car*> pPorsche; 這樣好些 :p static_cast 轉型的話,會作 compile-time check。 也就是說,如果你要把 a 轉成 b, 而 a, b 是沒有任何關係的 object 則在編譯時,就會有 error message
static_cast 不作 runtime-check, 一但出錯就是.... 當!
3. 先將 pCar 轉回成 Porsche, 再呼叫 Porsche 的 member function - maxSpeed()
PS: C++ 的轉型還有 * dynamic_cast 跟 static_cast 的差別就是會作runtime check, 如果無法轉型的話,會傳回 NULL
* reinterpret_cast 我的感覺就是跟 C-Style 轉型一樣,但好像有點小區別.. 這個好像有加上一點限制
此文章被評分,最近評分記錄財富:30 (by codeboy) | 理由: 感謝幫忙講解喔~^^ | |
|
|
|