By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

why define virtual destructor

P: n/a
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
Aug 11 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> 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

Aug 11 '05 #2

P: n/a
Tony Johansson wrote:
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
Aug 11 '05 #3

P: n/a
Sorry, I messed up:

Kai-Uwe Bux wrote:
Tony Johansson wrote:
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.


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


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

Aug 11 '05 #4

P: n/a

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.

Aug 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.