→ freesamael:會變成 NULL 08/13 01:38
→ freesamael:改成B b=dynamic_cast<B>(*obj)會有bad_cast exception 08/13 01:39
→ QQ29:喔喔想起來了 dynamic cast失敗會return null而不是exception 08/13 01:40
→ tinlans:所謂的安全是建立在你有檢查 cast 結果的前提下。 08/13 01:40
→ tinlans:二樓的 <> 裡面應該是 B & 吧,ref 就會丟 exception。 08/13 01:41
→ tinlans:我不知道有幾年沒寫到 dynamic_cast 了。 08/13 01:42
→ freesamael:對耶,筆誤 08/13 01:42
→ tinlans:至於 invoke 下去才會發出 exception,我想那不是 C++ 的 08/13 01:44
→ tinlans:exception,應該是 OS 丟出來的。 08/13 01:44
→ tinlans:至於那個算不算 exception,要問熟 Windows 的。 08/13 01:44
→ QQ29:null->QQ();這樣是OS丟出來的唷!? 08/13 01:45
→ QQ29:B &b=dynamic_cast<B&>(*obj); 請教如果改這樣 f大寫的 B b= 08/13 01:45
→ QQ29:差異在哪裡阿 我實在不懂為啥一定要轉成ref再做assign 08/13 01:46
→ QQ29:為了通過dynamic_cast語法的規定嗎? 08/13 01:46
推 LPH66:null-> 這個是 OS 丟出來的沒錯啊 不合法記憶體存取 08/13 02:03
→ freesamael:dynamic_cast<T>, T必須為pointer或reference 08/13 02:55
→ freesamael:你可以用 B b = dynamic_cast<B&>(*obj); 08/13 02:56
→ QQ29:所以是為了滿足dynamic cast的語法嗎? 08/13 13:34
→ tinlans:雖然不明白你所謂滿足語法的意義是什麼,不過... 08/13 13:39
→ tinlans:C++ 的多型機制只會作用在 pointer 跟 reference 上。 08/13 13:39
→ tinlans:用 by value 傳遞只會造成物件被切割。 08/13 13:40
→ tinlans:另外因為 reference 不能重新指向其它東西, 08/13 13:40
→ tinlans:也不能指向無物,所以為了表示轉型失敗,才用 exception。 08/13 13:41
→ tinlans:pointer 就沒這問題,所以轉型失敗可以直接傳回 0 表示。 08/13 13:41