I know it is a good practice to declare virtual destructors for base classes in C++, but is it always important to declare virtual destructors even for abstract classes that function as interfaces? Please provide some reasons and examples why.
I know it is a good practice to declare virtual destructors for base classes
Share
It’s even more important for an interface. Any user of your class will probably hold a pointer to the interface, not a pointer to the concrete implementation. When they come to delete it, if the destructor is non-virtual, they will call the interface’s destructor (or the compiler-provided default, if you didn’t specify one), not the derived class’s destructor. Instant memory leak.
For example