473,503 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why define virtual destructor

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
4 8419
> 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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
10493
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
7
1875
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
37
4121
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
26
3945
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
8
2563
by: Pete C | last post by:
In this section of the FAQ: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10 an abstract destructor is given a definition with the cryptic comment "It's faster this...
5
11335
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
7
3077
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
7
1978
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
16
3061
by: jeff_rowa | last post by:
Hello people, I know that in a base class you should define the destructor as virtual but is there any cases that you define a destructor as virtual. In the case when you implement assignment...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.