173

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?

1
  • 11
    It may compile, but does it link? Jul 25, 2014 at 0:24

2 Answers 2

229

Yes. You also need to implement the destructor:

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

inline A::~A() { }

should suffice.

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/

13
  • 16
    Um, yes it is. Pure only means a derived class also needs to provide an implementation.
    – MSN
    Mar 10, 2009 at 16:24
  • 79
    Implementing pure virtual functions is in fact legal. Very useful for providing a default implementation but forcing subclasses to call it explicitly. Mar 10, 2009 at 16:47
  • 6
    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) Mar 10, 2009 at 17:04
  • 2
    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
    Mar 11, 2009 at 17:47
  • 11
    A better way to put it is that once you declare a destructor, it is not automatically implemented for you.
    – MSN
    Mar 11, 2009 at 21:36
49

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
};
9
  • 17
    +1. I think Herb Sutter also has some good info on this: gotw.ca/gotw/031.htm. It's interesting to note that any pure virtual function may have an implementation provided, not just destructors. Mar 10, 2009 at 16:55
  • 6
    Yes, it's something you do in an interview to freak your interviewers ;)
    – dirkgently
    Mar 10, 2009 at 16:59
  • 1
    It's actually not all that uncommon, in my experience.
    – anon
    Mar 10, 2009 at 17:44
  • @Neil Butterworth: Which one?
    – dirkgently
    Mar 10, 2009 at 17:49
  • @Dirk - the "any function" scenario. It's not uncommon to find it used to implement some common behaviour.
    – anon
    Mar 10, 2009 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.