Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why define virtual destructor

Reply
Thread Tools

why define virtual destructor

 
 
Tony Johansson
Guest
Posts: n/a
 
      08-11-2005
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


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-11-2005
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
 
Reply With Quote
 
 
 
 
Srini
Guest
Posts: n/a
 
      08-11-2005
> 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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-11-2005
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

 
Reply With Quote
 
garth_rockett@yahoo.com
Guest
Posts: n/a
 
      08-11-2005

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? sojin C++ 12 04-07-2006 09:02 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM
Virtual destructor for virtual base class? Chunhui Han C++ 2 06-24-2004 10:13 AM
virtual destructor Vs virtual method Calvin Lai C++ 7 12-18-2003 03:11 AM



Advertisments