再说c++虚析构函数

关于c++类中的虚析构函数。我写这篇博客是为了得出如下3点结论。

1.所有基类的析构函数,都应该声明为虚析构函数!这也是c++标准所指定的。
2.如果设计一个类,可能会被后来的其他类所继承,我们一定要将它的析构函数声明为虚析构。否则被继承会出现内存泄漏等意想不到的问题。
3.如果我们要去继承另外一个类,首先一点是要保证被继承的类的析构函数已经声明为了虚析构函数!
对以上3点,如果你深有理会,如下内容可以不看,不要浪费时间。如果没有很深的概念,可以参考一下。如有问题,希望在评论区或其他方式给我指点,谢谢。
 
 

非继承类的析构函数执行会有如下操作

1、执行析构函数中的代码

2、如果所在类有成员变量(类对象),再执行类成员变量的析构函数

普通析构函数:

以如下代码为例

class Student
{
public:
    Student()
    {
        name = new char[32];
        cout << "Student constructor new name char[32]" << endl;
    }
    ~Student()
    {
        delete name;
        cout << "Student destructor delete Student::name" << endl;
    }
private:
    char* name;
};
class Normal
{
public:
    Student stu;
    Normal(){cout << "Normal constructor" << endl;}
    ~Normal(){cout << "Normal destructor" << endl;}
};
int main()
{
    Normal* pn = new Normal();
    delete pn;
    return 0;
}
执行结果:

(表示很讨厌上面的水印)

析构顺序:

1、执行析构函数中的代码

2、如果所在类有成员变量(类对象),再执行类成员变量的析构函数



以上内容很好理解,因为只是单个类对象的析构,当然也关系到类的组合。
接下来我想讨论的是存在继承关系时,析构函数在做些什么?
#include <iostream>
using namespace std;
#include <string>
class Student
{
public:
    Student()
    {
        name = new char[32];
        cout << "Student constructor new name char[32]" << endl;
    }
    ~Student()
    {
        delete []name;
        cout << "Student destructor delete Student::name" << endl;
    }
private:
    char* name;
};
class Base
{
public:
    Base()
    {
        cout << "Base()" << endl;
    }
    ~Base()
    {
        cout << "~Base()" << endl;
    }
};
class Derived:public Base
{
public:
    Derived()
    {
        cout << "Derived()" << endl;
    }
    ~Derived()
    {
        cout << "~Derived()" << endl;
    }
    Student stu;
};
int main()
{
    Derived *pd = new Derived();
    delete pd;
    return 0;
}
执行结果:

派生类对象的析构过程:
1,调用派生类对象的析构函数,
2,调用派生类中成员对象的析构函数
3,调用基类的析构函数


-------------------------------------我是丑陋的分割线-------------------------------------------
 
在分割线的上方,一切正常,没有什么特别的,异样的地方。
接下来要讨论的是多态情况下的析构函数。
#include <iostream>
using namespace std;
#include <string>
class Student
{
public:
    Student()
    {
        name = new char[32];
        cout << "Student constructor new name char[32]" << endl;
    }
    ~Student()
    {
        delete []name;
        cout << "Student destructor delete Student::name" << endl;
    }
private:
    char* name;
};
class Base
{
public:
    Base()
    {
        cout << "Base()" << endl;
    }
    ~Base()
    {
        cout << "~Base()" << endl;
    }
};
class Derived:public Base
{
public:
    Derived()
    {
        cout << "Derived()" << endl;
    }
    ~Derived()
    {
        cout << "~Derived()" << endl;
    }
    Student stu;
};
int main()
{
    Base* pb = new Derived(); // 注意这一行,基类指针指向了派生类
    delete pb;  	      // delete 基类指针
    return 0;
}

执行结果:


只调用基类的析构函数,派生类的析构函数没有被调用,更要注意的是,因此派生类的成员对象也没有被析构,缺少了“Student destructor delete Student::name”
所以会有内存泄漏。
如下代码也会出现内存泄漏:
class Base
{
};
class Derived:public Base
{
public:
    string str; // 这种内存泄漏就不容易发现了!!!
};
int main()
{
    Base* pb = new Derived(); // 派生类的string对象会发生内存泄漏!
    delete pb;
    return 0;
}
如上类继承时发生的内存泄漏,如何解决?
只需要将基类的析构函数声明为虚析构函数,就是前面加上virtual.
#include <iostream>
using namespace std;
#include <string>
class Student
{
public:
    Student()
    {
        name = new char[32];
        cout << "Student constructor new name char[32]" << endl;
    }
    ~Student()
    {
        delete []name;
        cout << "Student destructor delete Student::name" << endl;
    }
private:
    char* name;
};
class Base
{
public:
    Base()
    {
        cout << "Base()" << endl;
    }
    virtual ~Base()
    {
        cout << "~Base()" << endl;
    }
};
class Derived:public Base
{
public:
    Derived()
    {
        cout << "Derived()" << endl;
    }
    ~Derived()
    {
        cout << "~Derived()" << endl;
    }
    Student stu;
};
int main()
{
    Base* pb = new Derived();
    delete pb;
    return 0;
}
运行结果:

发现一切OK了。
 
 
总结:
1.所有基类的析构函数,都应该声明为虚析构函数!这也是c++标准所指定的。
2.如果设计一个类,可能会被后来的其他类所继承,我们一定要将它的析构函数声明为虚析构。否则被继承会出现内存泄漏等意想不到的问题。
3.如果我们要去继承另外一个类,首先一点是要保证被继承的类的析构函数已经声明为了虚析构函数!
 
 
 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值