Re: [問題] 多型的自動回收

看板C_and_CPP (C/C++)作者 (CA)時間16年前 (2009/10/06 19:43), 編輯推噓2(204)
留言6則, 3人參與, 最新討論串3/13 (看更多)
※ 引述《littleshan (我要加入劍道社!)》之銘言: 我想我把我的 interface 勾出來會比較清楚一點: class Base { public: virtual ~Base() { try { close(); catch( ... ) { // ... } } void close() { // ... doClose(); // ... } private: virtual void doClose() = 0; }; class Derived { virtual void doClose() { // ... } }; ==== 也就是說在 Derived 解構完後 Base::~Base() Base::close() Base::doClose() <- 在這個 frame 會造成 pure virtual function call 我知道 destructor 是用 chain call, 而不是 override 所以才會有這個問題 -- 自High筆記(半荒廢) http://legnaleurc.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.205.248.119

10/06 20:41, , 1F
在建構子和解構子不要調用virtual function
10/06 20:41, 1F

10/06 20:59, , 2F
對 ... 我第一篇就有說了 ...
10/06 20:59, 2F

10/06 22:50, , 3F
Effective C++ Item 9 -- Never call virtual functions
10/06 22:50, 3F

10/06 22:50, , 4F
during construction/destruction
10/06 22:50, 4F

10/07 00:51, , 5F
我的問題很跳 tone 嗎?QQ
10/07 00:51, 5F

10/07 00:55, , 6F
第一篇我就有表明我知道不能呼叫 virtual function
10/07 00:55, 6F
文章代碼(AID): #1AoorB-W (C_and_CPP)
文章代碼(AID): #1AoorB-W (C_and_CPP)