關於繼承中的析構函式

2021-07-01 18:30:18 字數 604 閱讀 7890

class clxbase;

~clxbase() ;

virtual void dosomething() ;

};class clxderived : public clxbase;

~clxderived() ;

void dosomething() ;

};int main()

{clxderived *p =  new clxderived;    //情況1

//clxbase *p =  new clxderived;      //情況2

//p->dosomething();

delete p;

return 0;

(gcc編譯)如果是情況1,結果為:

output from the destructor of class clxderived!

output from the destructor of class clxbase!

如果是情況2,結果為:

output from the destructor of class clxbase!

解析:其實我也不是很能說得清楚,先留著,知道這麼個執行結果,以後明白了再補上。

關於繼承中的析構函式

include using namespace std class base 輸出 derived f base f 為什麼呼叫了基類的析構函式,虛函式表就改變了?這樣的問題,有乙個最實用的方法,就是去跟一下彙編的 當然了這需要你懂一點點的組合語言.問題的原因就是 當你呼叫基類的析構函式d.base...

關於析構函式

q1 析構函式是幹什麼的?a1 析構函式用來釋放物件所分配的資源。舉例來說,lock 類可能鎖定了乙個訊號量,那麼析構函式將釋放該訊號量。最常見的例子是,當建構函式中使用了new,那麼析構函式則使用delete。q2 物件的析構順序?a2 與建構函式相反,先構造的後析構。如 乙個物件陣列構造順序是0...

繼承中建構函式與析構函式

include using namespace std class y y int ii i ii class x public y 這個是繼承,在繼承的時候,x 類中含有的函式與y 函式相同,則將y類的此函式遮蔽掉,無論引數是否相同,只要函式一樣就將其遮蔽掉,class base1 base1 c...