Pure virtual destructor in C++

C++PolymorphismDestructorPure Virtual

C++ Problem Overview


Is it wrong to write:

class A {
public:
    virtual ~A() = 0;
};

for an abstract base class?

At least that compiles in MSVC... Will it crash at run time?

C++ Solutions


Solution 1 - C++

Yes. You also need to implement the destructor:

class A {
public:
    virtual ~A() = 0;
};

inline A::~A() { }

should suffice.

And since this got a down vote, I should clarify: If you derive anything from A and then try to delete or destroy it, A's destructor will eventually be called. Since it is pure and doesn't have an implementation, undefined behavior will ensue. On one popular platform, that will invoke the purecall handler and crash.

Edit: fixing the declaration to be more conformant, compiled with http://www.comeaucomputing.com/tryitout/

Solution 2 - C++

Private destructors: they will give you an error when you create an object of a derived class -- not otherwise. A diagnostic may appear though.

> 12.4 Destructors

> 6 A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined.

A class with a pure virtual destructor is an abstract class. Note well:

> 10.4 Abstract classes > > 2 A pure virtual function need be defined only if called with, or as if with (12.4), the qualified-id syntax (5.1).

> [Note:a function declaration cannot provide both a pure-specifier and a definition —end note ]

Taken straight from the draft:

struct C {
   virtual void f() = 0 { }; // ill-formed
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIvan KrechetovView Question on Stackoverflow
Solution 1 - C++MSNView Answer on Stackoverflow
Solution 2 - C++dirkgentlyView Answer on Stackoverflow