Re: [問題] 想請教一個C++考題
看板C_and_CPP (C/C++)作者loveflames (咕啾咕啾魔法陣)時間16年前 (2010/06/08 00:56)推噓0(0推 0噓 0→)留言0則, 0人參與討論串2/5 (看更多)
※ 引述《NcuCsie (哇哩)》之銘言:
: 標準答案為ABA,與我實際跑過的結果相符。我比較迷惑的是為什麼
: 執行解構子的過程只印了最後一個A呢?ptr此時指向的是B物件,不
: 是應該先執行物件B的解構子嗎?麻煩各位指正小弟的觀念了,謝謝!
C++的規格書有段話這樣寫
the static type shall be a base class of the dynamic type of the object
to be deleted and the static type shall have a virtual destructor or
the behavior is undefined
拿下面這個例子來說
A *ptr=new B;
A是static type
B是dynamic type
這時如果delete ptr,而A沒有virtual dtor時,這個行為沒定義
(不過通常是只執行base dtor啦)
--
C++ hello world標準寫法
http://nopaste.csie.org/fee97
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.134.226.149
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章