473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2039
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Koller | last post by:
Hi list, I have a small example which gives me "pure virtual function called". I'm calling a virtual function in the destructor of the base class, where the pointer used is effectively...
39
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
7
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...
1
by: qazmlp | last post by:
I have a class like this: class base { public: virtual ~base() throw(); // Other members }; Now, if I write a class like this:
37
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 ]
2
by: ilovecpp | last post by:
it's a little bit comlicated to me.
2
by: arun | last post by:
Hello Group, I have a class A, which has three other members and each member is of a different class type. If I don't create a destructor then the compiler will give me one and whenever...
7
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
by: Tonni Tielens | last post by:
I'm trying to create a pure virtual class describing an interface. Normally, when I do this I make the destructor pure virtual so that, even if there are no members in the class, it cannot be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.