473,554 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2051
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****@g mail.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 objektorientier ter Datenverarbeitu ng
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****@g mail.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
2170
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 pointing to a derived class, but the whole thing is in the process of being destroyed. Interesting for me is also, that I get the same error if I call...
39
736
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, Interf: class Interf { public:
7
1880
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 destructors. What I am expecting is, if the destructor is declared as virtual in base, the destructors of all its derived classes also should be...
1
1375
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
4127
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
1487
by: ilovecpp | last post by:
it's a little bit comlicated to me.
2
3940
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 class A is destroyed (whether on stack or on heap) the destructor of each different object will be called.
7
3078
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 item 7 for why this is virtual ...
7
7330
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 instantiated. The difference now is that I'm making a generic interface with template arguments. Template classes should be defined in the header...
0
7612
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7576
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5443
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3583
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3564
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.