Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How Does compiler implement virtual destructor ???

Reply
Thread Tools

How Does compiler implement virtual destructor ???

 
 
Juha Nieminen
Guest
Posts: n/a
 
      04-16-2008
Pallav singh wrote:
> Hi All
>
> How Does compiler implement virtual destructor ???
>
> as we all know if base class destructor is virtual..........then while
> wriiting statemnt like this
>
> Base * b = new Derived( );
> delete b;
>
> // it will call Deived class destructor Followed by Base Class
>
> How compiler does House-keeping for it in V-Table ??


It's most probably again one thing which the standard doesn't specify,
but which all compilers do in the same way:

If there exists any virtual functions in a class (eg. a virtual
destructor) the size of the class will be increased by a pointer.
Internally this pointer points to a virtual table which contains
pointers to the virtual functions at fixed offsets. When calling a
virtual function (eg. the destructor), what it actually does is that it
takes that pointer, indexes it with a fixed value, takes the function
pointer found there and calls that function. If it's a destructor, then
the destructor will call the destructor of its own base class, etc.
 
Reply With Quote
 
 
 
 
Pallav singh
Guest
Posts: n/a
 
      04-16-2008
Hi All

How Does compiler implement virtual destructor ???

as we all know if base class destructor is virtual..........then while
wriiting statemnt like this

Base * b = new Derived( );
delete b;

// it will call Deived class destructor Followed by Base Class

How compiler does House-keeping for it in V-Table ??

Thanks
Pallav Singh
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      04-16-2008
Pallav singh a écrit :
> How Does compiler implement virtual destructor ???
>
> as we all know if base class destructor is virtual..........then while
> wriiting statemnt like this
>
> Base * b = new Derived( );
> delete b;
>
> // it will call Deived class destructor Followed by Base Class
>
> How compiler does House-keeping for it in V-Table ??


The destructor of the derived class will be called like any other
virtual function, that is why if you don't make it virtual, you will not
destroy the most derived object.

When delete calls:
b->~Base();

it will call:
b->~Derived();

Michael
 
Reply With Quote
 
raof01
Guest
Posts: n/a
 
      04-16-2008
Hello Pallav,

> How Does compiler implement virtual destructor ???
> as we all know if base class destructor is virtual..........then while
> wriiting statemnt like this
> Base * b = new Derived( );
> delete b;
> // it will call Deived class destructor Followed by Base Class How
> compiler does House-keeping for it in V-Table ??
>


Compiler does the same to virtual destructor as it does to other virtual
functions.

In destructor (maybe), compiler inserts some code to free the space hold
by VTABLE.

raof01
(E-Mail Removed)
"Thoughts are but dreams till their effects be tried." -- William Shakespeare


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-16-2008
On Apr 16, 10:08 am, Michael DOUBEZ <(E-Mail Removed)> wrote:
> Pallav singh a écrit :


> > How Does compiler implement virtual destructor ???


> > as we all know if base class destructor is
> > virtual..........then while wriiting statemnt like this


> > Base * b = new Derived( );
> > delete b;


> > // it will call Deived class destructor Followed by Base Class


> > How compiler does House-keeping for it in V-Table ??


> The destructor of the derived class will be called like any
> other virtual function, that is why if you don't make it
> virtual, you will not destroy the most derived object.


If you don't make it virtual, you'll have undefined behavior.
It might not destruct the most derived object, it might not free
the memory at all, it might crash, or it might cause a subtle
bug to appear later in your code.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-16-2008
raof01 wrote:
> Compiler does the same to virtual destructor as it does to other virtual
> functions.


Not *exactly* the same. In the case of virtual functions only the
function of the most derived class (of the type of the object) is
called. In the case of destructors, *all* destructors are called in the
inheritance hierarchy, not just one.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
How does the compiler implement a virtual destructor? ilovecpp@gmail.com C++ 2 12-16-2005 04:16 PM
Format of compiler generated derived destructor when base has 'virtual ~base() throw():" qazmlp C++ 1 04-10-2005 03:09 PM
virtual destructor Vs virtual method Calvin Lai C++ 7 12-18-2003 03:11 AM



Advertisments