20

Possible Duplicates:
Under what circumstances is it advantageous to give an implementation of a pure virtual function?
Why do we need a pure virtual destructor in C++?

Compiler doesn't force the Child class to implement a destructor when its Base has pure virtual destructor.

struct Base
{
  virtual void foo () = 0;
  virtual ~Base() = 0;
};
Base::~Base() {} // necessary

struct Child : Base
{
  void foo() {}
  //ok! no destructor needed to create objects of 'Child'
};

Funny part is that; compiler rather forces the Base to define a destructor body. Which is understood. (Demo for reference)

Then what is the purpose of having pure virtual destructor in Base class ? (Is it just to disallow Base creating objects?)

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • `Child` has a defaulted constructor. – pmr Jul 28 '11 at 09:29
  • @Luc, that answer is already mentioned in my question. Wanted to know if any other purpose – iammilind Jul 28 '11 at 09:31
  • the answer is already here http://stackoverflow.com/questions/1219607/why-do-we-need-a-pure-virtual-destructor-in-c – celavek Jul 28 '11 at 09:50
  • @iammilind: sorry, but I don't fully understand the question. There are obvious goals to have virtual destructor (correct delete, for instance). We'd want to transfer this property to descendats, and declare virtual dtor in Base. However, we have nothing to do in this ~Base(). Why we need to provide any kind of dummy dtor's body? So having pure virtual dtor seems quite natural. Or the question is rather about "how it works without explicit dtor in Child?") ? Or I'm missing something? – user396672 Jul 28 '11 at 11:12

4 Answers4

29

Sometimes an abstract base class has no virtual methods (= often called a “mixin”) or no methods at all (= often called a “type tag”).

To force those classes to be used as abstract base classes, at least one method needs to be pure virtual – but the classes don’t have virtual methods! So we make the destructor pure virtual instead.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
15

It makes the class abstract. The existance of at least a pure virtual method is sufficient for a class to be abstract.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

If you don't have any other pure virtual functions in Base, you have the option to make the destructor pure virtual so that the base class is still abstract.

It actually does force the derived class to implement a destructor, but the compiler will do that for you if you don't provide one.


Ok, maybe I could have phrased this better. The second paragraph is in reply to:

Compiler doesn't force the Child class to implement a destructor when its Base has pure virtual destructor.

I probably wanted to say that a virtual destructor (pure or not) causes the derived class to also have a virtual destructor, whether you write it or the compiler does.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • `It actually does force the derived class to implement a destructor,` and `but the compiler will do that for you if you don't provide one.` are contradicting each other – iammilind Jul 28 '11 at 09:34
  • @iammilind: They are not contradictory. The class is required to have a destructor. The compiler provides a destructor. Good. – Steve Jessop Jul 28 '11 at 09:58
  • 1
    Strictly speaking, Bo is wrong: the presence of a pure virtual destructor in the base doesn't force the derived class to implement a destructor; the simple fact that it is a class forces a destructor. All classes have a destructor. Always. – James Kanze Jul 28 '11 at 10:49
2

Compiler doesn't force the Child class to implement a destructor when its Base has pure virtual destructor.

No, Compiler does generate a default destructor for Child class (which in turn calls the implementation of pure virtual destructor of class Base) in case you don't define one explicitly.

RoundPi
  • 5,819
  • 7
  • 49
  • 75