码迷,mamicode.com
首页 > 编程语言 > 详细

再说c++虚析构函数

时间:2016-04-29 17:47:02      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

关于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.如果我们要去继承另外一个类,首先一点是要保证被继承的类的析构函数已经声明为了虚析构函数!



再说c++虚析构函数

标签:

原文地址:http://blog.csdn.net/hzhsan/article/details/51246734

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!