What is the purpose of pure virtual destructor? [duplicate]
Asked Answered
W

4

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?)

Whereof answered 28/7, 2011 at 9:25 Comment(4)
Child has a defaulted constructor.Gyrostatic
@Luc, that answer is already mentioned in my question. Wanted to know if any other purposeWhereof
the answer is already here #1220107Separable
@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?Prattle
C
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.

Chatter answered 28/7, 2011 at 9:31 Comment(0)
J
15

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

Jeremie answered 28/7, 2011 at 9:29 Comment(0)
C
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.

Creepy answered 28/7, 2011 at 9:32 Comment(3)
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 otherWhereof
@iammilind: They are not contradictory. The class is required to have a destructor. The compiler provides a destructor. Good.Frolic
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.Lecia
A
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.

Argumentum answered 28/7, 2011 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.