TITLE: Pure virtual destructors PROBLEM: krames@dbai.tuwien.ac.at (Gerfried Krames) 1) Is the following declaration legal? class X { ... public: X(); virtual ~X() = 0; }; RESPONSE: rmartin@rcmcon.com (Robert Martin), 17 May 95 Pure virtual destructors are an interesting aspect of C++. If a function is pure virtual, then the class that contains it is abstract, and C++ will not allow it to be instantiated. Purity is inherited. Thus: struct B {virtual void f() = 0;}; struct D : B {}; // abstract class, pure virtual f is inherited. However, destructors are *not* inherited. Thus: struct B {virtual ~B() = 0;}; struct D : B {} // concrete class. Compiler generates destructor. All classes must have a destructor. If one is not specified, then the compiler will write one for you. If your destructor is pure virtual, you still have to supply an implementation. Thus: struct B {virtual ~B() = 0;} B::~B() {/* do whatever */} If you don't implement the pure virtual destructor, you will get a link error. ------------------------------------- What all this adds up to is this. A pure virtual destructor is exactly like a regular virtual destructor, except that it makes the class it is declared in, abstract. It does not affect derived classes at all. In particular, it does not make it a compiler error to omit destructor declarations from derived classes. Thus, I use pure virtual destructors as a way of making a class abstract, if that class has no other pure virtual functions.