设为首页 加入收藏

TOP

再说c++虚析构函数(一)
2016-04-27 17:25:16 】 浏览:938
Tags:再说 函数

关于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 
   
using namespace std;
#include 
   
 
  
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 
   
using namespace std;
#include 
   
 
  
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 
   
using namespace std;
#include 
   
 
  
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.如果设计一个类,可能会被后来的其他类所继承
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[C++]资源管理 下一篇笔试题40. LeetCode OJ (27)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目