virtual解構函式(作用)
virtual解構函式的作用? .
大家知道,解構函式是為了在物件不被使用之後釋放它的資源,虛擬函式是為了實現多型。那麼把解構函式宣告為vitual有什麼作用呢?請看下面的程式碼:
1 #include 2 using namespace std; 3 4 class Base 5 { 6 public: 7 Base() {}; //Base的建構函式 8 ~Base() //Base的解構函式 9 { 10 cout << "Output from the destructor of class Base!" << endl; 11 }; 12 virtual void DoSomething() 13 { 14 cout << "Do something in class Base!" << endl; 15 }; 16 }; 17 18 class Derived : public Base 19 { 20 public: 21 Derived() {}; //Derived的建構函式 22 ~Derived() //Derived的解構函式 23 { 24 cout << "Output from the destructor of class Derived!" << endl; 25 }; 26 void DoSomething() 27 { 28 cout << "Do something in class Derived!" << endl; 29 }; 30 }; 31 32 int main() 33 { 34 Derived *pTest1 = new Derived(); //Derived類的指標 35 pTest1->DoSomething(); 36 delete pTest1; 37 38 cout << endl; 39 40 Base *pTest2 = new Derived(); //Base類的指標 41 pTest2->DoSomething(); 42 delete pTest2; 43 44 return 0; 45 }
先看程式輸出結果:
1 Do something in class Derived!
2 Output from the destructor of class Derived!
3 Output from the destructor of class Base!
4
5 Do something in class Derived!
6 Output from the destructor of class Base!
程式碼第36行可以正常釋放pTest1的資源,而程式碼第42行沒有正常釋放pTest2的資源,因為從結果看Derived類的解構函式並沒有被呼叫。通常情況下類的解構函式裡面都是釋放記憶體資源,而解構函式不被呼叫的話就會造成記憶體洩漏。原因是指標pTest2是Base型別的指標,釋放pTest2時只進行Base類的解構函式。在程式碼第8行前面加上virtual關鍵字後的執行結果如下:
1 Do something in class Derived!
2 Output from the destructor of class Derived!
3 Output from the destructor of class Base!
4
5 Do something in class Derived!
6 Output from the destructor of class Derived!
7 Output from the destructor of class Base!
此時釋放指標pTest2時,由於Base的解構函式是virtual的,就會先找到並執行Derived類的解構函式,然後再執行Base類的解構函式,資源正常釋放,避免了記憶體洩漏。
因此,只有當一個類被用來作為基類的時候,才會把解構函式寫成虛擬函式。
原文:http://blog.csdn.net/han_348154920/article/details/5944351