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

How Does compiler implement virtual destructor ???

P: n/a
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
Share this Question
Share on Google+
6 Replies


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

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

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

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

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

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