爲何基類的析構函數是虛函數?

1.第一段代碼ios

#include<iostream>
using namespace std;
class ClxBase{
public:
	ClxBase() {};
	~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; };

	void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase{
public:
	ClxDerived() {};
	~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };

	void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };
};
int   main(){
	ClxDerived *p = new ClxDerived;
	p->DoSomething();
	delete p;
	return 0;
}

  

運行結果:函數

Do something in class ClxDerived!            spa

Output from the destructor of class ClxDerived!指針

Output from the destructor of class ClxBase!  對象

    這段代碼中基類的析構函數不是虛函數,在main函數中用繼承類的指針去操做繼承類的成員,釋放指針P的過程是:先釋放繼承類的資源,再釋放基類資源. blog

 

2.第二段代碼繼承

#include<iostream>
using namespace std;
class ClxBase{
public:
	ClxBase() {};
	~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; };

	void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase{
public:
	ClxDerived() {};
	~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };

	void DoSomething() { cout << "Do something in class ClxDerived!" << endl; }
};
int   main(){
	ClxBase *p = new ClxDerived;
	p->DoSomething();
	delete p;
	return 0;
}

  

輸出結果:內存

Do something in class ClxBase!
Output from the destructor of class ClxBase!資源

    這段代碼中基類的析構函數一樣不是虛函數,不一樣的是在main函數中用基類的指針去操做繼承類的成員,釋放指針P的過程是:只是釋放了基類的資源,而沒有調用繼承類的析構函數.調用dosomething()函數執行的也是基類定義的函數.編譯器

    通常狀況下,這樣的刪除只可以刪除基類對象,而不能刪除子類對象,造成了刪除一半形象,形成內存泄漏.

    在公有繼承中,基類對派生類及其對象的操做,只能影響到那些從基類繼承下來的成員.若是想要用基類對非繼承成員進行操做,則要把基類的這個函數定義爲虛函數.

    析構函數天然也應該如此:若是它想析構子類中的從新定義或新的成員及對象,固然也應該聲明爲虛的. 

 

3.第三段代碼:

#include<iostream>
using namespace std;
class ClxBase{
public:
	ClxBase() {};
	virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; };
	virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase{
public:
	ClxDerived() {};
	~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };
	void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };
};

int   main(){
	ClxBase *p = new ClxDerived;
	p->DoSomething();
	delete p;
	return 0;
}

  

運行結果:

Do something in class ClxDerived!
Output from the destructor of class ClxDerived!
Output from the destructor of class ClxBase!

    這段代碼中基類的析構函數被定義爲虛函數,在main函數中用基類的指針去操做繼承類的成員,釋放指針P的過程是:只是釋放了繼承類的資源,再調用基類的析構函數.調用dosomething()函數執行的也是繼承類定義的函數. 

 

    若是不須要基類對派生類及對象進行操做,則不能定義虛函數,由於這樣會增長內存開銷.當類裏面有定義虛函數的時候,編譯器會給類添加一個虛函數表,裏面來存放虛函數指針,這樣就會增長類的存儲空間.因此,只有當一個類被用來做爲基類的時候,才把析構函數寫成虛函數.

相關文章
相關標籤/搜索