why define virtual destructor

T

Tony Johansson

Hello Experts!!

Assume I have a base class called animal. I want this class to be abstract
so I make the destructor pure virtual by having this statement.
virtual ~Animal() = 0;

Destructor will never be inherited.

Now to my question why do I have to give a body {} at least the empty body
to the pure virtual destructor.

What's the difference between
virtual ~Animal() = 0; and virtual ~Animal() = 0 {};

Normally when you have pure virtual destructors you almost never give any
kind of body to the function.

Many thanks.

//Tony
 
K

Kai-Uwe Bux

Tony said:
Hello Experts!!

Assume I have a base class called animal. I want this class to be abstract
so I make the destructor pure virtual by having this statement.
virtual ~Animal() = 0;

Destructor will never be inherited.
Huh?

Now to my question why do I have to give a body {} at least the empty body
to the pure virtual destructor.

You don't.
What's the difference between
virtual ~Animal() = 0; and virtual ~Animal() = 0 {};

virtual ~Animal() = 0;
is valid C++.

virtual ~Animal() = 0 {};
will not compile.
Normally when you have pure virtual destructors you almost never give any
kind of body to the function.

"almost"?


Best

Kai-Uwe Bux
 
S

Srini

Hello Experts!!
Assume I have a base class called animal. I want this class to be abstract
so I make the destructor pure virtual by having this statement.
virtual ~Animal() = 0;

Destructor will never be inherited.

Now to my question why do I have to give a body {} at least the empty body
to the pure virtual destructor.

This has been discussed earlier too...
It is essential to define the pure virtual destructor of a base class.
This is because, all the destructors in a class heirarchy are called
during the destruction of a derived class object. If you could leave
off the definition of a pure virtual destructor, what body will the
compiler execute during destruction?
What's the difference between
virtual ~Animal() = 0; and virtual ~Animal() = 0 {};

The second way of defining a pure virtual destructor is illegal.

virtual ~Animal() = 0;

Animal::~Animal() {
// ...
}
Normally when you have pure virtual destructors you almost never give any
kind of body to the function.

No. In fact, you have to provide a body to the pure virtual destructor.
Otherwise you'll get linker errors - the linker would complain about
an undefined reference to the destructor whenever there's a derived
class object being destroyed.
Many thanks.

//Tony

Srini
 
K

Kai-Uwe Bux

Sorry, I messed up:

Kai-Uwe Bux said:
You don't.

Actually, you do because C++ standard (12.4/7) says you shall at least if
your program is going to create objects of type Animal (or any derived
type). Now, if you would not want to create such objects, why would you
have the class in the first place?
virtual ~Animal() = 0;
is valid C++.

virtual ~Animal() = 0 {};
will not compile.


"almost"?

Goes for pure virtual functions (otherwise, why make them pure in the first
place), but destructors are special: they get called from destructors of
derived classes. Thus, they better be defined even for an abstract base
class. (That seems to be the rational behind 12.4/7.)


Best

Kai-Uwe
 
G

garth_rockett

In general, you are allowed to give a body for any pure virtual
function that you might define. You normally don't. If you do, your
derived classes can call this code. You make pure virtual functions to
create an interface only and require that individual implementation
classes all inherit from it. However, if you want to provide a default
implementation (which derived classes can call) along with your
abstract base class, you do it using the body of the pure virtual
function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top