Re: [問題] 多型的自動回收
你要不要試著用 aggregation
class Base {
public:
Base( BaseInterface *entity) { content= entity; }
void open() { content->doOpen(); }
void close() { content->doClose(); }
~Base() { content->doClose(); delete content; }
};
class BaseInterface {
private:
friend Base;
virtual void doOpen()= 0;
virtual void doClose()= 0;
};
blah
※ 引述《legnaleurc (CA)》之銘言:
: OK, 也許我描述得不夠清楚, 現在綜合兩篇的內容
: class Base {
: public:
: void open() {
: // ...
: this->doOpen();
: // ...
: this->opening_ = true;
: }
: void close() {
: // ...
: this->doClose();
: // ...
: this->opening_ = false;
: }
: virtual ~Base() {
: // this->close();
: assert( !this->opening_ );
: }
: private:
: virtual void doOpen() = 0;
: virtual void doClose() = 0;
: bool opening_;
: };
: class Derived {
: virtual void doOpen() {}
: virtual void doClose() {}
: };
: ====
: 我就是希望在 Base 這個層級就能在解構時自動回收
: 但是礙於 destructor 內不能呼叫 virtual function
: 目前我只有檢查 flag
: 我只想問在這個狀況下有沒有比較好的做法
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.124.99.126
→
10/07 14:17, , 1F
10/07 14:17, 1F
→
10/07 14:18, , 2F
10/07 14:18, 2F
推
10/07 14:44, , 3F
10/07 14:44, 3F
→
10/07 14:46, , 4F
10/07 14:46, 4F
推
10/07 15:46, , 5F
10/07 15:46, 5F
→
10/07 15:46, , 6F
10/07 15:46, 6F
→
10/07 15:49, , 7F
10/07 15:49, 7F
→
10/07 15:50, , 8F
10/07 15:50, 8F
推
10/07 16:02, , 9F
10/07 16:02, 9F
→
10/07 16:03, , 10F
10/07 16:03, 10F
→
10/07 16:03, , 11F
10/07 16:03, 11F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章