(C++)淺談多態基類解構函式聲明為虛函數

來源:互聯網
上載者:User

標籤:

主要內容:

1、C++類繼承中的建構函式和解構函式

2、C++多態性中的靜態繫結和動態綁定

3、C++多態性中解構函式聲明為虛函數

 

1、C++類繼承中的建構函式和解構函式

在C++的類繼承中,

建立對象時,首先調用基類的建構函式,然後在調用下一個衍生類別的建構函式,依次類推;

析構對象時,其順序正好與構造相反;

具體參考文章:http://www.cnblogs.com/AndyJee/p/4575385.html

 

2、C++多態性中的靜態繫結和動態綁定

對象的靜態類型:對象在聲明是採用的類型,在編譯期確定;

對象的動態類型:當前對象所指的類型,在運行期決定,對象的動態類型可以更改,但靜態類型無法更改。

靜態繫結:綁定的是對象的靜態類型,某特性(比如函數)依賴於對象的靜態類型,發生在編譯期。
動態綁定:綁定的是對象的動態類型,某特性(比如函數)依賴於對象的動態類型,發生在運行期。

具體參考文章:http://www.cnblogs.com/AndyJee/p/4575670.html

 

3、C++多態性中基類解構函式聲明為虛函數

先來看幾段程式例子:

  • 將基類解構函式聲明為虛函數
#include <iostream>using namespace std;class Person{public:    virtual ~Person(){  //declare destructor as a virtual function    cout << "Person::~Person()" << endl;    }};class Student : public Person{public:    ~Student(){     // virtual or not is OK        cout << "Student::~Student()" << endl;    }};int main(){    Person *pt1 = new Person;    Person *pt2 = new Student;        // base class pointer point to derived class    // Student *pt3 = new Person;     // derived class pointer can not point to base class    Student *pt4 = new Student;    delete pt1;    cout << "*********" << endl;    delete pt2;    cout << "*********" << endl;    //delete pt3;    //cout << "*********" << endl;    delete pt4;    cout << "*********" << endl;    return 0;}

運行結果:

  • 不將基類解構函式聲明為虛函數:
#include <iostream>using namespace std;class Person{public:    ~Person(){  //declare destructor as a virtual function    cout << "Person::~Person()" << endl;    }};class Student : public Person{public:    ~Student(){     // virtual or not is OK        cout << "Student::~Student()" << endl;    }};int main(){    Person *pt1 = new Person;    Person *pt2 = new Student;        // base class pointer point to derived class    // Student *pt3 = new Person;     // derived class pointer can not point to base class    Student *pt4 = new Student;    delete pt1;    cout << "*********" << endl;    delete pt2;    cout << "*********" << endl;    //delete pt3;    //cout << "*********" << endl;    delete pt4;    cout << "*********" << endl;    return 0;}

運行結果:

 

可以看出:

在用基類指標指向衍生類別時,

在基類解構函式聲明為virtual的時候,delete基類指標,會先調用衍生類別的解構函式,再調用基類的解構函式。

在基類解構函式沒有聲明為virtual的時候,delete基類指標,只會調用基類的解構函式,而不會調用衍生類別的解構函式,這樣會造成銷毀對象的不完全。

分析:

Person *pt2 = new Student;

pt2的靜態類型為Person,而動態類型為Student,

當解構函式為虛函數時,為動態綁定,delete pt2,會調用動態類型即衍生類別的解構函式,由於繼承關係,也會調用基類的解構函式;

而當解構函式為非虛函數時,為靜態繫結,delete pt2,會調用靜態類型即基類的解構函式,而不會調用衍生類別的解構函式。

(以上純屬個人理解)

 

總結:
  • 應該為多態基類聲明虛析構器。一旦一個類包含虛函數,它就應該包含一個虛析構器,因為多態性,必定會有基類調用衍生類別。

  • 如果一個類不用作基類或者不需具有多態性,便不應該為它聲明虛析構器。

 

參考文章:

http://www.cnblogs.com/children/archive/2012/08/13/2636956.html

(C++)淺談多態基類解構函式聲明為虛函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.