How Does compiler implement virtual destructor ???

J

Juha Nieminen

Pallav said:
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.
 
P

Pallav singh

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
 
M

Michael DOUBEZ

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
 
R

raof01

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:[email protected]
"Thoughts are but dreams till their effects be tried." -- William Shakespeare
 
J

James Kanze

Pallav singh a écrit :
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.
 
J

Juha Nieminen

raof01 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top