Lets take this example:
/* Source.cpp */
struct Base {
virtual void func() = 0;
};
struct Derived : public Base {
virtual void func() override { }
};
int main() {
return 0;
}
Upon compilation (on g++ 8.3.0) via g++ Source.cpp -Wnon-virtual-dtor
we get this:
Source.cpp:2:8: warning: 'struct Base' has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]
struct Base {
^~~~
Source.cpp:6:8: warning: base class 'struct Base' has accessible non-virtual destructor [-Wnon-virtual-dtor]
struct Derived : public Base {
^~~~~~~
Source.cpp:6:8: warning: 'struct Derived' has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]
What is the practical thing to do?
- Add
virtual ~Base() = default;
to every such classBase
to get rid of the warning (downside is we are adding boilerplate code, which inherently doesn't seem to do anything). - Or is there an argument to remove
-Wnon-virtual-dtor
and leave the code untouched? (downside is we are potentially allowing memory leaks, or are we) - I guess a question worth asking is, why doesn't the compiler create a virtual default destructor if the class has virtual members?