Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

delete this

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 19 Oct 2007 08:19 GMT
Hello everyone,

Just interested to learn how C++ implements delete this statement
internally. From logical point of view, I can not imagine how to implement a
component which destroy itself.

What makes me confused is the component is relying on itself even in the
process of destroy, how could it destroys itself from itself?

thanks in advance,
George
QbProg - 19 Oct 2007 08:31 GMT
Internally it is simple.

delete this
=
call ThisClass::~ThisClass () ; // Destructor
free (this) ;  // deallocation

It's like you're calling from anywhere else.
Of course , "delete this" must be the last statement using object
members(data or function) in the procedure you are in, because "this"
pointer is no more valid after the deletion.

delete this is usually usefoul with reference counted objects (as in
COM), but I won't suggest it's use anywhere else. You could probably
see a "delete this" in the Release function of COM objects.

Good bye
QbProg
Carl Daniel [VC++ MVP] - 19 Oct 2007 15:32 GMT
> Internally it is simple.
>
[quoted text clipped - 11 lines]
> COM), but I won't suggest it's use anywhere else. You could probably
> see a "delete this" in the Release function of COM objects.

One other thing to mention about 'delete this' - it's treading very close to
undefined behavior, but it's a commonly used idiom.  The key to having it
work is that 'delete this' must be the very last statement executed in the
function.  Any further use of the object afterwards has undefined behavior.

-cd
George - 19 Oct 2007 15:51 GMT
Thanks cd,

Your experience is very helpful.

regards,
George

> > Internally it is simple.
> >
[quoted text clipped - 18 lines]
>
> -cd
George - 19 Oct 2007 15:48 GMT
Thanks QbProg,

Your reply is helpful.

regards,
George

> Internally it is simple.
>
[quoted text clipped - 14 lines]
> Good bye
> QbProg

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.