Question

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?

Was it helpful?

Solution

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/

OTHER TIPS

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
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top