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

what happens internally

P: n/a

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

Dec 13 '05 #1
Share this Question
Share on Google+
2 Replies


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

Dec 13 '05 #2

P: n/a

swesoc wrote:
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.

Dec 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.