Common wisdom is that if you can make a class abstract by having a pure virtual destructor.
To quote Herb Sutter:
All base classes should have a virtual destructor (see your favourite C++ book for the reasons). If the class should be abstract (you want to prevent instantiating it) but it doesn't happen to have any other pure virtual functions, a common technique to make the destructor pure virtual:
However the following code compiles for me with GCC and VC:
#include <iostream>
struct base {
virtual ~base() = 0;
};
base::~base() { std::cout << "base destructor\n"; }
struct derived : base { };
int main() {
derived d;
}
Has something changed in C++11 that I'm not aware of?
BTW the motivation to this question is an answer I gave five years ago and was suddenly challenged by a commenter.
The derived
class has an implicitly defined (compiler-provided) virtual destructor, which is not pure and which overrides the base destructor. For this reason derived
is not an abstract class. It can be instantiated.
This has nothing to do with C++11. That's how it has always been since C++98. Making a base class destructor pure virtual is a way to make that and only that class abstract. The compiler-provided destructors in derived classes will be non-pure virtual, which will automatically "cancel out" that abstractness in those classes (assuming no other pure virtual methods were inherited from the base).