简体   繁体   中英

Which base class's virtual destructor is overriden in a derived class

I'm confused about overriding functions when the derived class isn't immediately derived, but derived from an already derived class.

#include <iostream>

struct Base
{ 
    virtual ~Base() { std::cout << "Base destructor called\n"; }
};

struct Derived : Base
{
    Derived() {}
    // Implicitly supplied destructor, I'm guessing. Which is also virtual?
};

struct MostDerived : Derived
{
    MostDerived(){};
    ~MostDerived() { std::cout << "MostDerived destructor called\n"; }
};

int main()
{
    Derived* d = new MostDerived();
    delete d;
    Base* b = new MostDerived();
    delete b;
}

In both cases MostDerived's destructor is called. I'm wondering if it's only required that the most base class has a destructor declared virtual, and in that case all other classes inheriting from it all have virtual destructors that override every other destructor further upstream, if you get my meaning.

I'm not sure if I made sense, basically if I had a series of 10 classes with each one inheriting from the last one, any destructor in the chain will override all the destructors that are more base than it?

struct GreatGrandfather{~virtual GreatGrandfather(){}}; // Only this is necessary
struct Grandfather : GreatGrandfather {};
struct Father : Grandfather{};
struct Son : Father{};
struct Grandson : Son {};
struct GreatGrandson : Grandson{};

Grandson's destructor will override all of the classes above it, but not GreatGrandson's destructor?

And also, once a base class's destructor or other function is declared virtual none of its descendants need to be declared virtual again?

any destructor in the chain will override all the destructors that are more base than it?

Yes, pretty much. A destructor will be implicitly supplied by the implementation if you don't write one. So it will also implicitly override the base class d'tor.

Grandson's destructor will override all of the classes above it, but not GreatGrandson's destructor?

It will override Son 's d'tor. Which by extension overrides Father 's and so forth. So yes. And it can't override GreatGrandson , since at the point of its definition it cannot look into the future and know GreatGrandson will exist.

And also, once a base class's destructor or other function is declared virtual none of its descendants need to be declared virtual again?

Yes, same as with any virtual function. Once declared virtual, it's always virtual in any derived class.

A destructor overrides all virtual destructors of base classes, including indirect bases. Also, a function that overrides a virtual function is itself virtual. Therefore, yes, the implicitly defined destructor of Derived is virtual, and both ~Base and ~Derived are overridden by ~MostDerived . When a pointer to a base class is used to destroy an object of derived class type, the destructor that needs to be virtual is that of the base class. (The one in the derived class will then be implicitly virtual.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM