class xxxx has virtual method but non-virtual destructor

主要内容:

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++多态性中基类析构函数声明为虚函数

先来看几段程序例子:

  • 将基类析构函数声明为虚函数

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#include

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;

}

运行结果:

class xxxx has virtual method but non-virtual destructor_第1张图片

  • 不将基类析构函数声明为虚函数:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#include

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;

}

运行结果:

class xxxx has virtual method but non-virtual destructor_第2张图片

 

可以看出:

在用基类指针指向派生类时,

在基类析构函数声明为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++开发的时候,用来做基类的类的析构函数一般都是虚函数。可是,为什么要这样做呢?下面用一个小例子来说明:        有下面的两个类:

class ClxBase{public:    ClxBase() {};    virtual ~ClxBase() {};    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; };};

    代码

ClxBase *pTest = new ClxDerived;pTest->DoSomething();delete pTest;

    的输出结果是:
Do something in class ClxDerived!Output from the destructor of class ClxDerived!
    这个很简单,非常好理解。    但是,如果把类ClxBase析构函数前的virtual去掉,那输出结果就是下面的样子了:
Do something in class ClxDerived!
    也就是说,类ClxDerived的析构函数根本没有被调用!一般情况下类的析构函数里面都是释放内存资源,而析构函数不被调用的话就会造成内存泄漏。我想所有的C++程序员都知道这样的危险性。当然,如果在析构函数中做了其他工作的话,那你的所有努力也都是白费力气。    所以,文章开头的那个问题的答案就是--这样做是为了当用一个基类的指针删除一个派生类的对象时,派生类的析构函数会被调用。    当然,并不是要把所有类的析构函数都写成虚函数。因为当类里面有虚函数的时候,编译器会给类添加一个虚函数表,里面来存放虚函数指针,这样就会增加类的存储空间。所以,只有当一个类被用来作为基类的时候,才把析构函数写成虚函数。
————————————————
版权声明:本文为CSDN博主「StarLee」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/starlee/article/details/619827

你可能感兴趣的:(C++,异常处理)