基类与接口类中的虚析构函数(virtual destructor)

一般情况下,在基类或者接口类中添加虚析构函(virtual destructor)数非常重要。原因很简单,就是想让析构的顺序从继承类开始往上开始一步步析构直到基类。

看例子吧:

首先,接口类不写虚析构函数

// virtual d'tor
// In base class or interface, it is important that add a virtual destructor
class IFoo
{
public:
    virtual void dosomething() = 0;
};

class Foo : public IFoo
{
public:
    Foo(int* pInt = NULL) : pInt(pInt){}
    virtual void dosomething()
    {
        pInt = new int[10];
        pInt[1] = 1;
        pInt[2] = 2;
    }
    ~Foo()
    {
        delete[] pInt;
    }

    int* get_pInt()
    {
        return pInt;
    }

    const int* get_pInt() const
    {
        return pInt;
    }

private:
    int* pInt;
};

去调用测试一下:

using namespace std;
int main()
{
    IFoo* f = new Foo();
    f->dosomething();
    delete f;

    return 0;
}

当执行

delete f;

时,程序并不会调用Foo类中的析构函数(因为你没在基类中写虚析构函数),所以造成内存泄露。

下面我们加上virtual destructor

class IFoo
{
public:
    virtual void dosomething() = 0;
    virtual ~IFoo(){}  // add virtual destructor
};

class Foo : public IFoo
{
public:
    Foo(int* pInt = NULL) : pInt(pInt){}
    virtual void dosomething()
    {
        pInt = new int[10];
        pInt[1] = 1;
        pInt[2] = 2;
    }
    ~Foo()
    {
        delete[] pInt;
    }

    int* get_pInt()
    {
        return pInt;
    }

    const int* get_pInt() const
    {
        return pInt;
    }

private:
    int* pInt;
};
using namespace std;
int main()
{
    IFoo* f = new Foo();
    f->dosomething();
    delete f;   // It's ok!!!

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++,如果一个类函数,我们通常都会将它的析构函数设为虚析构函数虚析构函数是指在基类析构函数声明为函数,这样在删除指向派生类对象的基类指针时,会调用派生类的析构函数。 需要虚析构函数的主要原因是防止内存泄漏。当我们在使用多态时,通常会使用基类指针来指向派生类对象,这时如果析构函数不是函数,删除指向派生类对象的基类指针时,只会调用基类析构函数,而不会调用派生类的析构函数。这就会导致派生类申请的动态内存无法被释放,从而造成内存泄漏。 使用虚析构函数可以保证在删除指向派生类对象的基类指针时,会先调用派生类的析构函数,从而保证所有动态内存都能正确释放。 举个例子,假设我们有一个基类 Animal 和一个派生类 Cat。Animal 类有一个指针类型的成员变量,指向一个动态分配的字符串。Cat 类继承自 Animal 类,并且重载了析构函数。如果 Animal 类的析构函数不是函数,那么在删除指向 Cat 对象的 Animal 指针时,只会调用 Animal 类的析构函数,从而导致 Cat 类申请的动态内存无法被释放,造成内存泄漏。 ```c++ class Animal { public: Animal() { name = new char[20]; strcpy(name, "Animal"); } ~Animal() { delete[] name; cout << "Animal destructor" << endl; } protected: char* name; }; class Cat : public Animal { public: Cat() { name = new char[20]; strcpy(name, "Cat"); } ~Cat() { delete[] name; cout << "Cat destructor" << endl; } }; int main() { Animal* p = new Cat(); delete p; // Animal destructor,没有调用 Cat 的析构函数,造成内存泄漏 return 0; } ``` 如果将 Animal 类的析构函数声明为虚析构函数,那么在删除指向 Cat 对象的 Animal 指针时,就会先调用 Cat 类的析构函数,从而正确释放动态内存。 ```c++ class Animal { public: Animal() { name = new char[20]; strcpy(name, "Animal"); } virtual ~Animal() { // 声明为虚析构函数 delete[] name; cout << "Animal destructor" << endl; } protected: char* name; }; class Cat : public Animal { public: Cat() { name = new char[20]; strcpy(name, "Cat"); } ~Cat() { delete[] name; cout << "Cat destructor" << endl; } }; int main() { Animal* p = new Cat(); delete p; // Cat destructor,然后 Animal destructor return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值