13

I read in other threads that when you implement a pure virtual destructor (yes it can have an implementation) it must be empty, and should (?) be inline. Should it be empty? If so, why? Should it be inline? If so, why?

Edit: This is how a pure virtual descructor can have an implementation:

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

inline A::~A(){
    //implementation
}
EpsilonVector
  • 3,973
  • 7
  • 38
  • 62
  • How can something that is "pure virtual" have an implementation? – tenfour Feb 14 '11 at 23:06
  • Can you provide a link to the other threads that mention this? It would help us decipher what was meant. – James Feb 14 '11 at 23:07
  • 1
    If that class is inherited, it should have a virtual destructor, but pure virtual destructor doesn't make much sense. – Cat Plus Plus Feb 14 '11 at 23:08
  • If your destructor is a [pure virtual function](http://en.wikipedia.org/wiki/Virtual_function#C.2B.2B_2), then it can't have an implementation. – bits Feb 14 '11 at 23:11
  • 1
    Seems like this question has some interesting information: http://stackoverflow.com/questions/1219607/why-do-we-need-a-pure-virtual-destructor-in-c – Nim Feb 14 '11 at 23:12
  • 15
    @bits: Incorrect. Any pure virtual function can have a definition. In most cases, it would only be called in unusual circumstances. But a destructor must have a definition, even if it is pure virtual. – aschepler Feb 14 '11 at 23:15

4 Answers4

25

A pure virtual destructor must have an implementation (assuming you have at least one concrete derived class).

There is no rule that a pure virtual destructor must have empty body. Nor do I know of any reason that it should, except the same reasons most destructors should have an empty body.

A pure virtual destructor can be inline or non-inline. I would expect the benefits of each to depend on the number of base classes and non-static members with non-trivial destructors.

One other catch, though: on certain popular compilers, if the destructor is the only virtual method defined for the class, it is a good idea to make it non-inline, to help the implementation deal with its polymorphism magic.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 2
    which "certain popular compilers" are you talking about? i find this claim hard to believe. please give a concrete example. – Cheers and hth. - Alf Feb 14 '11 at 23:16
  • @Alf: In g++ it makes a slight difference, but is no big deal. Still, I believe the writers of the g++ standard libraries chose to make some destructors non-inline for this reason. – aschepler Feb 14 '11 at 23:22
  • 2
    in concrete terms, what's the problem with g++? and is there a bug report for that? – Cheers and hth. - Alf Feb 14 '11 at 23:25
  • 2
    The problem (with many compilers) is that at Translation Unit level, the compiler doesn't know whether the destructor is needed, so it has to create copies in every object file. The linker can then eliminate all those copies. This is unavoidable in any fully seperate compilation model. You need some link-time compilation to solve it. – MSalters Feb 15 '11 at 08:57
  • 1
    There is no g++ bug. It's just that if a polymorphic class has no non-inline virtual members, object files for TUs which include the class definition are slightly larger and linking takes a little longer. – aschepler Feb 15 '11 at 13:00
  • @aschepler Is it a recommendation to defined pure virtual destructor implementation. because code is compiled successfully without implementation. – EmptyData Aug 10 '16 at 09:22
8

Sounds strange.

Firstly, "yes it can have an implementation" is a rather strange remark. It is actually required to have an implementation (at least in C++98). There's no way around it. Anyone who ever used a pure virtual destructor knows it.

Secondly, it is not supposed to always be empty. It is simply supposed to do what it needs to do. If you have nothing to do there explicitly, leave it empty. Otherwise, it won't be empty.

Finally, it can be inline or non-inline - it makes no difference. What is true is that it cannot be defined in class definition. The definition must be out-of-class one (inline or not - does not matter).

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • I said "yes it can have an implementation" because not everybody know that a pure virtual function can have an implementation and I anticipated people start asking me about it, so I put that disclaimer. Didn't help. – EpsilonVector Feb 14 '11 at 23:11
5

You need implementation of pure virtual destructor because, the way virtual destructor works is that the most derived class's destructor is called first, then the destructor of each base class is called. It means compilers will generate a call to base class pure virtual destructor even though the class is abstract, so you must provide a body for the function. If you don't provide body, the linker will complain about a missing symbol.

There might be a case, when you want your base class to be abstract even if your class does not contain any pure virtual function. Here declaring pure virtual destructor will make your class abstract. In this case, your destructor can have empty body. To avoid paying overhead cost of a call to an empty body destructor,declare it as inline.

CreativeMind
  • 897
  • 6
  • 19
3

Keeping in mind that 'must' and 'should' mean different things...

A pure virtual destructor can be non-empty. Don't know who might have said otherwise.

Should it be? Yes, since an abstract base should not have anything to delete. You will find that you'll violate this latter 'should' from time to time and thus, of course, may the former as well.

Should it be inline? It neither should nor should not. I can't think of anything you clearly gain by it being "inline" since implementations are both free to ignore inlines and to inline non-inlines. Generally though there's no reason to create an implementation file for nothing more than an empty destructor in an abstract class.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125