Search code examples
c++c++17clang-tidy

`virtual` `override` destructor


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.


Solution

  • 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.