0

In the following example:

class A {
public:
    virtual ~A() { std::cout << "~A" << std::endl; }
};

class B : public A {
public:
    virtual ~B() override { std::cout << "~B" << std::endl; }
};

class C : public B {
public:
    ~C() override { std::cout << "~C" << std::endl; }
};

clang-tidy gives the following warning for class B:

'virtual' is redundant since this function is already declared 'override'

Removing the virtual keyword from class B seems to allow all destructors in the chain to be called, but I want to make sure that I'm not missing anything.

rhughes
  • 9,257
  • 11
  • 59
  • 87
  • 1
    Does this answer your question? [virtual? override? or both? C++ \[duplicate\]](https://stackoverflow.com/questions/39932391/virtual-override-or-both-c) – 273K Jan 21 '20 at 01:29
  • I was thinking of this specifically because this is regarding the destructor; that post mentions standard functions. Are they the same in this regard? – rhughes Jan 21 '20 at 01:32
  • 1
    Once a method (including a destructor) has been marked as `virtual`, it is implicitly `virtual` in all descendants, so explicitly stating the `virtual` keyword again in those descendants is redundant, hence the warning. And applying `override` on a method *requires* the base method to be virtual (implicitly or explicitly) since only virtual methods can be overridden. – Remy Lebeau Jan 21 '20 at 01:34
  • @RemyLebeau So once a function is virtual, it is always virtual all the way down the chain? – rhughes Jan 21 '20 at 01:35
  • @rhughes yes, exactly – Remy Lebeau Jan 21 '20 at 01:35

1 Answers1

1

Removing virtual from function that has override does not change the meaning of the program in any way. That is what it means for the keyword to be redundant (in that context). The removal doesn't allow anything that isn't allowed without the removal.

eerorika
  • 232,697
  • 12
  • 197
  • 326