tags:

views:

1950

answers:

2

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?

+17  A: 

Yes. You also need to implement the destructor:

class A {
    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 (which isn't exactly possible since the destructor is private). 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/

MSN
So it should be declared virtual ~A() { }, without the "= 0". It's not pure virtual if you provide an implementation
jalf
Um, yes it is. Pure only means a derived class also needs to provide an implementation.
MSN
Implementing pure virtual functions is in fact legal. Very useful for providing a default implementation but forcing subclasses to call it explicitly.
jmucchiello
MSN and note if you have that definition in the header, you need to put "inline" before it to avoid violating the ODR (one definition rule)
Johannes Schaub - litb
Why does A::~A() have to be explicitly defined, since, I thought there is a default destructor for every object? As in any type of inheritance, the chain of destructors is always called and the base class destructor doesn't have to always be defined.
jeffD
The base class destructor always has to be defined if it will be invoked. If you never delete or explicitly destroy sublcasses of A, this doesn't matter. If you do, it does.
MSN
A better way to put it is that once you declare a destructor, it is not automatically implemented for you.
MSN
+10  A: 

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
};
dirkgently
+1. I think Herb Sutter also has some good info on this: http://www.gotw.ca/gotw/031.htm. It's interesting to note that any pure virtual function may have an implementation provided, not just destructors.
Fred Larson
Yes, it's something you do in an interview to freak your interviewers ;)
dirkgently
It's actually not all that uncommon, in my experience.
anon
@Neil Butterworth: Which one?
dirkgently
@Dirk - the "any function" scenario. It's not uncommon to find it used to implement some common behaviour.
anon
@Neil: Ah, I see. I do use that often enough. Very handy.
dirkgently