KyaPoocha.com

Huge Collection of Interview Questions


What happens when you make call “delete this;” ?? 

The code has two built-in pitfalls. First, if it executes in a member function for an
extern, static, or automatic object, the program will probably crash as soon as the delete
statement executes. There is no portable way for an object to tell that it was instantiated
on the heap, so the class cannot assert that its object is properly instantiated. Second,
when an object commits suicide this way, the using program might not know about its demise.
As far as the instantiating program is concerned, the object remains in scope and continues
to exist even though the object did itself in. Subsequent dereferencing of the pointer can
and usually does lead to disaster.
You should never do this. Since compiler does not know whether the object was allocated on
the stack or on the heap, “delete this” could cause a disaster.



If you liked this question/asnwer, make sure you
Subscribe to KyaPoocha.com RSS feed!


BOOKMARK THIS : BlinkList | del.icio.us | Digg it | Furl | reddit | StumbleUpon | Yahoo MyWeb |