我是否需要显式调用基本虚拟析构函数?

时间:2009-03-24 14:22:10

标签: c++ destructor

当用C ++覆盖一个类(带有虚拟析构函数)时,我在继承类上再次将析构函数实现为虚拟,但是我是否需要调用基础析构函数?

如果是这样,我想它就是这样......

MyChildClass::~MyChildClass() // virtual in header
{
    // Call to base destructor...
    this->MyBaseClass::~MyBaseClass();

    // Some destructing specific to MyChildClass
}

我是对的吗?

7 个答案:

答案 0 :(得分:417)

不,以相反的构造顺序自动调用析构函数。 (基础课最后)。不要调用基类析构函数。

答案 1 :(得分:86)

不,您不需要调用基础析构函数,派生析构函数总是为您调用基础析构函数。 Please see my related answer here for order of destruction

要了解为什么要在基类中使用虚拟析构函数,请参阅以下代码:

class B
{
public:
    virtual ~B()
    {
        cout<<"B destructor"<<endl;
    }
};


class D : public B
{
public:
    virtual ~D()
    {
        cout<<"D destructor"<<endl;
    }
};

当你这样做时:

B *pD = new D();
delete pD;

然后,如果B中没有虚拟析构函数,则只调用~B()。但是因为你有一个虚拟的析构函数,所以先调用~D(),然后调用~B()。

答案 2 :(得分:26)

其他人说了什么,但也注意到你不必在派生类中声明析构函数virtual。一旦声明了析构函数virtual,就像在基类中那样,所有派生的析构函数都是虚拟的,无论你是否声明它们。换句话说:

struct A {
   virtual ~A() {}
};

struct B : public A {
   virtual ~B() {}   // this is virtual
};

struct C : public A {
   ~C() {}          // this is virtual too
};

答案 3 :(得分:9)

没有。与其他虚拟方法不同,您将显式调用Derived中的Base方法来“调用”链接,编译器会生成代码,以按照调用构造函数的相反顺序调用析构函数。

答案 4 :(得分:7)

不,你永远不会调用基类析构函数,它总是像其他人指出的那样自动调用,但这里是结果的概念证明:

String[][] items = {
         {"1 st", "2 nd", "3 rd", "4 th"},
         {"12 th Sept", "13 th Sept", "14 th Sept", "15 th Sept"},
};

输出结果为:

class base {
public:
    base()  { cout << __FUNCTION__ << endl; }
    ~base() { cout << __FUNCTION__ << endl; }
};

class derived : public base {
public:
    derived() { cout << __FUNCTION__ << endl; }
    ~derived() { cout << __FUNCTION__ << endl; } // adding call to base::~base() here results in double call to base destructor
};


int main()
{
    cout << "case 1, declared as local variable on stack" << endl << endl;
    {
        derived d1;
    }

    cout << endl << endl;

    cout << "case 2, created using new, assigned to derive class" << endl << endl;
    derived * d2 = new derived;
    delete d2;

    cout << endl << endl;

    cout << "case 3, created with new, assigned to base class" << endl << endl;
    base * d3 = new derived;
    delete d3;

    cout << endl;

    return 0;
}

如果将基类析构函数设置为虚拟的,那么案例3的结果将与案例1&amp; 2。

答案 5 :(得分:6)

没有。它会被自动调用。

答案 6 :(得分:0)

只有在将基类析构函数声明为virtual 时,C ++的析构函数会自动按照其构造顺序被调用(先派生然后派基)。 >

如果没有,那么在删除对象时仅调用基类析构函数。

示例:没有虚拟析构函数

#include <iostream>

using namespace std;

class Base{
public:
  Base(){
    cout << "Base Constructor \n";
  }

  ~Base(){
    cout << "Base Destructor \n";
  }

};

class Derived: public Base{
public:
  int *n;
  Derived(){
    cout << "Derived Constructor \n";
    n = new int(10);
  }

  void display(){
    cout<< "Value: "<< *n << endl;
  }

  ~Derived(){
    cout << "Derived Destructor \n";
  }
};

int main() {

 Base *obj = new Derived();  //Derived object with base pointer
 delete(obj);   //Deleting object
 return 0;

}

输出

Base Constructor
Derived Constructor
Base Destructor

示例:使用基本虚拟析构函数

#include <iostream>

using namespace std;

class Base{
public:
  Base(){
    cout << "Base Constructor \n";
  }

  //virtual destructor
  virtual ~Base(){
    cout << "Base Destructor \n";
  }

};

class Derived: public Base{
public:
  int *n;
  Derived(){
    cout << "Derived Constructor \n";
    n = new int(10);
  }

  void display(){
    cout<< "Value: "<< *n << endl;
  }

  ~Derived(){
    cout << "Derived Destructor \n";
    delete(n);  //deleting the memory used by pointer
  }
};

int main() {

 Base *obj = new Derived();  //Derived object with base pointer
 delete(obj);   //Deleting object
 return 0;

}

输出

Base Constructor
Derived Constructor
Derived Destructor
Base Destructor

建议将基类析构函数声明为virtual,否则会导致未定义的行为。

参考:Virtual Destructor