delete this questions
-
It is my understanding the using the following line of code is bad pratice. delete this; Is there ever a good reason to use this construct? Thanks
with MFC, it's used to delete a modeless dialog.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
Okay so COM makes use of it and also with self deleting classes. I understand the COM aspect but what is a self deleting class? Let me clarify the question a little. Is there a general rule? When would you use delete this? Thanks
smesser wrote: what is a self deleting class? A class that deletes itself. Examples are CFrameWnd and CWinThread (when m_bAutoDelete is TRUE).
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
-
smesser wrote: what is a self deleting class? A class that deletes itself. Examples are CFrameWnd and CWinThread (when m_bAutoDelete is TRUE).
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
Yeah, but that is framework stuff. When as a programmer have you used: delete this; Steve
-
It is my understanding the using the following line of code is bad pratice. delete this; Is there ever a good reason to use this construct? Thanks
I have been programming with c++ since the Cfront "compiler" came out of ATT. I have never found a circumstance where delete this seemed like a correct solution. My impresssion is that it is an artifact of bad design. The fact that the MFC and COM implementation require it is not a surprise ;-) Along the same line, throwing an exception via a pointer is equally dangerous in my book!
-
Yeah, but that is framework stuff. When as a programmer have you used: delete this; Steve
http://www.codeproject.com/miscctrl/windowscroller.asp[^]
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
-
I have been programming with c++ since the Cfront "compiler" came out of ATT. I have never found a circumstance where delete this seemed like a correct solution. My impresssion is that it is an artifact of bad design. The fact that the MFC and COM implementation require it is not a surprise ;-) Along the same line, throwing an exception via a pointer is equally dangerous in my book!
Brian R wrote: it is an artifact of bad design It is a tool that makes it easier to use and then dispose of one time objects without having to keep track of the pointers and memory yourself. Using it is a design decision, not "bad design". Abusing it is bad design, but then abusing anything is.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
-
It is my understanding the using the following line of code is bad pratice. delete this; Is there ever a good reason to use this construct? Thanks
thanks all I now have a better understanding of this topic.
-
Brian R wrote: it is an artifact of bad design It is a tool that makes it easier to use and then dispose of one time objects without having to keep track of the pointers and memory yourself. Using it is a design decision, not "bad design". Abusing it is bad design, but then abusing anything is.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
PJ Arends wrote: Using it is a design decision, not "bad design". Abusing it is bad design, but then abusing anything is I am in 100% agreement with you. Even the infamous 'goto' is cursed as a bad design paradigm. But circumstances govern their use in a few specialized cases. After all 'delete this' has certainly come to my rescue when I had to monitor the lifetime of an object using a reference counter similar to the COM classes. I seem a fair use of 'delete this' in such case. Also there are so many things in C++ that if abused screw up the program, better curse programming skill than curse a language ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
PJ Arends wrote: Using it is a design decision, not "bad design". Abusing it is bad design, but then abusing anything is I am in 100% agreement with you. Even the infamous 'goto' is cursed as a bad design paradigm. But circumstances govern their use in a few specialized cases. After all 'delete this' has certainly come to my rescue when I had to monitor the lifetime of an object using a reference counter similar to the COM classes. I seem a fair use of 'delete this' in such case. Also there are so many things in C++ that if abused screw up the program, better curse programming skill than curse a language ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mayur Mahajan wrote: But circumstances govern their use in a few specialized cases. A paper put together in 1966 by BÖhm, Corrado, and Guiseppe proved theoretically that the GOTO statement was unnecessary; Dijkstra considered it harmful; and Knuth said it might be useful in some circumstances.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Mayur Mahajan wrote: But circumstances govern their use in a few specialized cases. A paper put together in 1966 by BÖhm, Corrado, and Guiseppe proved theoretically that the GOTO statement was unnecessary; Dijkstra considered it harmful; and Knuth said it might be useful in some circumstances.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
Well, the main point here is: We have razor sharp tools in the kit. If you can handle them, use it...else they'll hurt you bad ;P ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
It is my understanding the using the following line of code is bad pratice. delete this; Is there ever a good reason to use this construct? Thanks
You can use it in exception classes, which may be created on the stack or dynamically on the heap. When you call some Delete function, you can delete this if the object is created on the heap. I also got the blogging virus..[^]