c++为什么基类的析构函数要写成虚析构函数

 答:在实现多态时,当用基类操作派生类,在析构时防止只析构基类而不析构派生类的状况发生。

 

代码说明如下

第一段代码:

 1 #include<iostream>
 2 using namespace std;  
 3   
 4 class ClxBase  
 5 {public:  
 6    ClxBase() {}  
 7    ~ClxBase() {cout << "Output from the destructor of class ClxBase!" << endl}  
 8   
 9    void DoSomething() { cout << "Do something in class ClxBase!" << endl; }  
10 };  
11   
12 class ClxDerived : public ClxBase  
13 {public:   
14    ClxDerived() {}  
15    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl}  
16   
17    void DoSomething() { cout << "Do something in class ClxDerived!" << endl; }  
18 };   
19   
20 int main()  
21 {  
22    ClxDerived *p =  new ClxDerived;   
23    p->DoSomething();   
24    delete p;   
25   
26    return 0;   
27 }  

运行结果:

Do something in class ClxDerived!   

Output from the destructor of class ClxDerived!

Output from the destructor of class ClxBase!  

 

这段代码中基类的析构函数不是虚函数,在main函数中用继承类的指针去操作继承类的成员,释放指针P的过程是:先释放继承类的资源,再释放基类资源. 

 

第二段代码:

 1 #include<iostream>
 2 using namespace std;  
 3   
 4 class ClxBase  
 5 {public:   
 6    ClxBase() {}  
 7    ~ClxBase() {cout << "Output from the destructor of class ClxBase!" << endl}  
 8   
 9    void DoSomething() { cout << "Do something in class ClxBase!" << endl}  
10 };  
11   
12 class ClxDerived : public ClxBase  
13 {public:   
14    ClxDerived() {}  
15    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl }  
16   
17    void DoSomething() { cout << "Do something in class ClxDerived!" << endl}  
18 };  
19   
20 int main()  
21 {   
22    ClxBase *p =  new ClxDerived;   
23    p->DoSomething();   
24    delete p;   
25   
26    return 0;   
27 }   

输出结果:

Do something in class ClxBase! 

Output from the destructor of class ClxBase!

 

 这段代码中基类的析构函数同样不是虚函数,不同的是在main函数中用基类的指针去操作继承类的成员,释放指针P的过程是:只是释放了基类的资源,而没有调用继承类的析构函数.调用dosomething()函数执行的也是基类定义的函数.

 

 一般情况下,这样的删除只能够删除基类对象,而不能删除子类对象,形成了删除一半形象,造成内存泄漏.

 

 在公有继承中,基类对派生类及其对象的操作,只能影响到那些从基类继承下来的成员.如果想要用基类对非继承成员进行操作,则要把基类的这个函数定义为虚函数.

析构函数自然也应该如此:如果它想析构子类中的重新定义或新的成员及对象,当然也应该声明为虚的. 

 

第三段代码:

 1 #include<iostream>  
 2 using namespace std;  
 3 class ClxBase  
 4 {public:   
 5    ClxBase() {}   
 6    virtual ~ClxBase() {cout << "Output from the destructor of class ClxBase!" << endl}  
 7   
 8    virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl}  
 9 };  
10   
11   
12 class ClxDerived : public ClxBase  
13 {public:   
14    ClxDerived() {}   
15    ~ClxDerived() { cot << "Output from the destructor of class ClxDerived!" << endl}  
16   
17    void DoSomething() { cout << "Do something in class ClxDerived!" << endl}  
18 };  
19   
20 int   main()  
21 {  
22    ClxBase *p = new ClxDerived;  
23    p->DoSomething();   
24    delete p;  
25   
26    return 0;  
27 }  

运行结果:

Do something in class ClxDerived!

Output from the destructor of class ClxDerived!

Output from the destructor of class ClxBase!

 

这段代码中基类的析构函数被定义为虚函数,在main函数中用基类的指针去操作继承类的成员,释放指针P的过程是:只是释放了继承类的资源,再调用基类的析构函数.调用dosomething()函数执行的也是继承类定义的函数.  

 

如果不需要基类对派生类及对象进行操作,则不能定义虚函数,因为这样会增加内存开销.当类里面有定义虚函数的时候,编译器会给类添加一个虚函数表,里面来存放虚函数指针,这样就会增加类的存储空间.所以,只有当一个类被用来作为基类的时候,才把析构函数写成虚函数.

 

构造函数为什么不能是虚函数呢?

    首先需要了解 vptr指针和虚函数表的概念,以及这两者的关联。

    vptr指针指向虚函数表,执行虚函数的时候,会调用vptr指针指向的虚函数的地址。

    当定义一个对象的时候,首先会分配对象内存空间,然后调用构造函数来初始化对象。vptr变量是在构造函数中进行初始化的。又因为执行虚函数需要通过vptr指针来调用。如果可以定义构造函数为虚函数,那么就会陷入先有鸡还是先有蛋的循环讨论中。

 

基类的析构函数为什么必须是虚函数呢?

    我们都知道,想要回收一个对象申请的资源,那么就需要调用析构函数。虽然我们没有显示地调用析构函数,但是编译器都会默认地为我们执行析构函数。

    那么当我们执行 BaseClass *base = new BaseClass(); 当我们执行 delete base时,会调用析构函数为我们释放资源。而 我们执行BaseClass *sub = new SubClass(); 如果BaseClass基类的析构函数不是虚函数的时候,delete sub 对象的时候,只会释放BaseClass 基类申请的资源,而不是释放SubClass派生类的资源。原因如下:

    基类指针指向了派生类对象,而基类中的析构函数是非virtual的,而虚构函数是动态绑定的基础。现在析构函数不是virtual的,因此不会发生动态绑定,而是静态绑定,指针的静态类型为基类指针,因此在delete的时候只会调用基类的析构函数,而不会调用派生类的析构函数。这样,在派生类中申请的资源就不会得到释放,就会造成内存泄漏,这是相当危险的:如果系统中有大量的派生类对象被这样创建和销毁,就会有内存不断的泄漏,久而久之,系统就会因为缺少内存而崩溃。

    当然,如果在派生类中没有动态申请有资源的时候,是不会造成内存泄漏的。而当派生类对象的析构函数中有内存需要回收,并且在编程过程中采用了基类指针指向派生类对象,如为了实现多态,并且通过基类指针将对象销毁,这时,就会因为基类的析构函数为非虚函数而不触发动态绑定,从而没有调用派生类的析构函数而导致内存泄漏。

    因此,为了防止这种情况下的内存泄漏的发生,最后将基类的析构函数写成virtual虚析构函数。    

原文链接: https://www.cnblogs.com/xietianjiao/p/12859095.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++为什么基类的析构函数要写成虚析构函数

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/197132

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午7:28
下一篇 2023年2月12日 下午7:28

相关推荐