-
November 21st, 2007, 01:44 PM
#1
What will happen if I say delete this
Hi guys
Can someone please explain what will happen if I say:
Thank you....
-
November 21st, 2007, 02:40 PM
#2
Re: What will happen if I say delete this
It depends entirely on the context in which you call it, and can range from exactly what you want it to do all the way to a disastrous crash.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 21st, 2007, 03:37 PM
#3
Re: What will happen if I say delete this
Graham is correct. And actually, if you call this in a non-member function you will get a compiler error as well. ![Roll Eyes (Sarcastic)](images/smilies/rolleyes.gif)
Anyways...
If the object was allocated on the heap... It will call it's destructor, then free it. (And that's assuming the delete call matches the new call)
If it's an object on the stack, it will call the destructor.... And depending on the allocator used for new/delete calls when you call delete, this could crash, it might not. If this is a POD (plain old data) object, it probably will have no apparent effect if it does not crash on the delete call.
-
November 22nd, 2007, 04:12 AM
#4
Re: What will happen if I say delete this
... or you end up in an endless recursion leading to a stack overflow if you do this in the destructor itself.
-
November 22nd, 2007, 04:25 AM
#5
Re: What will happen if I say delete this
Please note that accessing any member after calling delete this result in undefined behaviour.
- Guido
-
November 22nd, 2007, 05:33 AM
#6
Re: What will happen if I say delete this
The statement delete this is used in COM when an object's reference count reaches zero.
-
November 22nd, 2007, 10:14 AM
#7
Re: What will happen if I say delete this
delete this; makes sense only when the object creation is being managed by the class itself. That is, the class creates objects of itself using new then returns to the caller and then exposes an interface that does the delete.
![Quote](images/misc/quote_icon.png) Originally Posted by JamesSchumacher
And depending on the allocator used for new/delete calls when you call delete, this could crash, it might not. If this is a POD (plain old data) object, it probably will have no apparent effect if it does not crash on the delete call.
Sorry, what does that mean?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
November 23rd, 2007, 06:05 AM
#8
Re: What will happen if I say delete this
Guys thank you very much !!!
I think I'm starting to get it.
Will some of you guys kindly enough, post a code examples for something that will work and the use of “delete this” is necessarily , and something that will crash the program. If it’s not too much hassle a brief explanation to the code will be great.
Once again THANK YOU!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|