what happens internally

S

swesoc

Hello Friend,

I know this question is been asked many times,

if a have base pointer pointing to derived object & if i delete that
pointer , if the base class destructor is not virtual. then what
happens internally ?

every 1 it gives an infinite behaviour, does anybody know what happens
exactly
 
N

Neelesh Bodas

swesoc said:
Hello Friend,

I know this question is been asked many times,

if a have base pointer pointing to derived object & if i delete that
pointer , if the base class destructor is not virtual. then what
happens internally ?
C.a.t.a.s.t.r.o.p.h.e


every 1 it gives an infinite behaviour, does anybody know what happens
exactly

Its not "infinite behaviour", it is "undefined behaviour". And by
undefined behaviour, we really mean u.n.d.e.f.i.n.e.d behaviour.
Anything may happen. Compiler is free to send mails to all people in
your address book.

On a more serious note, the compiler will not be able to understand
that the pointer points to an object of derived class. It might simply
destroy the base class portion, and not the derived class portion. This
will result in memory leaks, incomplete destruction of object, and what
not, collectively called as undefined behaviour.
 
A

Axter

swesoc said:
Hello Friend,

I know this question is been asked many times,

if a have base pointer pointing to derived object & if i delete that
pointer , if the base class destructor is not virtual. then what
happens internally ?

every 1 it gives an infinite behaviour, does anybody know what happens
exactly

(undefined) behaviour means anything can happen.

If you have a requirement to keep the base destructor non-virtual, you
can consider using the following smart pointer, which will correctly
call the derived class destructor (If the derived class type is passed
to the constructor).
http://code.axter.com/copy_ptr.h
Also cow poitner:
http://code.axter.com/cow_ptr.h

But I'm not advocating doing this unless you have a really good reason
for keeping the base class destructor non-virtual.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top