472,141 Members | 1,088 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,141 software developers and data experts.

How Does compiler implement virtual destructor ???

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
Jun 27 '08 #1
6 1941
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.
Jun 27 '08 #2
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
mailto:ra****@gmail.com
"Thoughts are but dreams till their effects be tried." -- William Shakespeare
Jun 27 '08 #3
On Apr 16, 10:08 am, Michael DOUBEZ <michael.dou...@free.frwrote:
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:ja*********@gmail.com
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
Jun 27 '08 #4
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.
Jun 27 '08 #5
On 2008-04-16 12:33:18 -0400, Juha Nieminen <no****@thanks.invalidsaid:
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.
Well, yes, that's how the destructor itself is implemented. But that
doesn't depend on whether the destructor is virtual. As far as
dispatching to the destructor goes, the mechanism is the same as for
any other virtual function.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #6
Hello Pete,
>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.
Well, yes, that's how the destructor itself is implemented. But that
doesn't depend on whether the destructor is virtual. As far as
dispatching to the destructor goes, the mechanism is the same as for
any other virtual function.
Yes, not exactly the same. To make it more clear, I meant that the dispatch
of virtual destructor is the same.

raof01
mailto:ra****@gmail.com
"Thoughts are but dreams till their effects be tried." -- William Shakespeare
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Martin Koller | last post: by
39 posts views Thread by Ele | last post: by
37 posts views Thread by WittyGuy | last post: by
7 posts views Thread by Tonni Tielens | last post: by

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.