0

I have a small question about virtual destructors. It's well known that if we have an abstract class A (a polymorphic class) we should declare the destructor of A virtual.

However, assume that we have B, a derived class of A, and C is a derived class of B. In B, we redefine all virtual methods of A to make sure that B is not again an abstract class.

My question is wether we should declare the destructor of B virtual or not for the sake of its derived class such as the class C.

struct A {
    virtual ~A() = default; // Good practice: virtual here

    virtual void foo() = 0;
};

struct B : A {
    virtual ~B() = default; // Is virtual required here?

    void foo() { }
};

struct C : B {

};

Thanks in advance!

dfrib
  • 70,367
  • 12
  • 127
  • 192
  • Duplicate indeed, in the sense that `virtual` "sticks" to destructors just likes it does to member functions. – Quentin Jan 11 '17 at 16:09
  • The question isn't an _exact_ duplicate. The question here is based on the misunderstanding that you have a choice, an assumption which isn't present in the linked question. But since any answer to that question also is an answer to this question, this question is still redundant. – MSalters Jan 11 '17 at 16:13
  • @MSalters that's what I'm aiming for, yes. Someone looking for "virtual destructor" will land here and be correctly redirected. – Quentin Jan 11 '17 at 16:15
  • Q&A related to the semantic choice of including `virtual` or not for `virtual` (by inheritance) methods in derived classes: [Implementing pure virtual function from abstract base class: does `override` specifier have any meaning?](http://stackoverflow.com/questions/39370403). – dfrib Jan 11 '17 at 16:18

0 Answers0