401

当在 C++ 中重写一个类(使用虚拟析构函数)时,我在继承类上再次将析构函数实现为虚拟,但是我需要调用基本析构函数吗?

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

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

    // Some destructing specific to MyChildClass
}

我对吗?

4

7 回答 7

540

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

于 2009-03-24T14:23:51.927 回答
98

不,您不需要调用基析构函数,派生析构函数始终为您调用基析构函数。 请在此处查看我的相关答案以了解销毁顺序

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

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()。

于 2009-03-24T14:23:20.477 回答
31

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

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

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

struct C : public A {
   ~C() {}          // this is virtual too
};
于 2009-03-24T14:29:34.363 回答
12

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

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;
}

输出是:

case 1, declared as local variable on stack

base::base
derived::derived
derived::~derived
base::~base


case 2, created using new, assigned to derive class

base::base
derived::derived
derived::~derived
base::~base


case 3, created with new, assigned to base class

base::base
derived::derived
base::~base

Press any key to continue . . .

如果您将基类析构函数设置为虚拟的,那么情况 3 的结果将与情况 1 和 2 相同。

于 2017-09-21T19:51:49.110 回答
12

只有在声明了基类析构函数时,C++中的析构函数才会自动按照它们的构造顺序(先派生然后基)被调用virtual

如果不是,则在删除对象时仅调用基类析构函数。

示例:没有虚拟析构函数

#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

示例:使用 Base 虚拟析构函数

#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否则会导致未定义的行为。

参考:虚拟析构函数

于 2019-10-11T14:16:32.833 回答
10

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

于 2009-03-24T14:34:07.427 回答
6

不,它是自动调用的。

于 2009-03-24T14:23:30.680 回答