|
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
|
|
|
|
|
Like anything else, if it is used properly it has its uses, but used improperly it can be disatrous. I use it for classes that are to be self deleting, and always make sure it is the very last command the class executes.
"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's how COM object lifetime is managed. Last one to call Release(), essentially tells the object to delete itself. For instance:
ULONG Release(void)
{
ULONG lResult = InterlockedDecrement(&m_dwRef);
if (lResult == 0) delete this;
return lResult;
}
|
|
|
|
|
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
|
|
|
|
|
Yeah, but that is framework stuff. When as a programmer have you used:
delete this;
Steve
|
|
|
|
|
|
with MFC, it's used to delete a modeless dialog.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
thanks all I now have a better understanding of this topic.
|
|
|
|
|
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..[^]
|
|
|
|
|
Hi
Can somebody please guide me how to wite an ISR in visual c++ 6.0. I need to handle some interrupts
Thanks
Leelanga
|
|
|
|
|
To my knowledge, ISRs haven't been used since DOS and 16-bit Windows. What exactly is it that you are trying to do?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I don't think that windows is polling for mouse, keyboard or any other hardware events...
But if you mean that the programmer has to use ISR's, you can't. I don't know since what OS version, but at least since Win2K those instuctions are "priviliged", meaning that they can only be executed in supervisor mode. Thats where device drivers come in.
I also got the blogging virus..[^]
|
|
|
|
|
Having never delved into a device driver project before, this is unfamilar territory for me. Thanks for the heads-up.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
You're welcome!
To be honest, I didn't work on a device driver project either, but I've worked on small embedded systems. Also I had a subject in college recently that focused on hardware and Windows.
I also got the blogging virus..[^]
|
|
|
|
|
Actualy I've developed a small h/w device to control telephone line. I use the paralell port to interface the device. So i need to hadle interrupts generated by it
Thanks a lot
Leelanga
|
|
|
|
|
|
Hi,
I need to get the absolute path for the given virtual folder path.
Consider an example given below. Windows has a virtual folder "My Documents" placed in Desktop.
So by using the path "C:\Documents and Settings\Administrator\Desktop\My Documents" can we get the Absolute ( Physical) path for the "My Documents"?
In my case the physical path for the "My Documents" is "C:\Documents and Settings\Administrator\My Documents".
Please send me the answer as soon as possible.
Thanks in Advance
vaidya
|
|
|
|
|
Check out the help on SHGetSpecialFolderPath() and SHGetFolderPath(); I've not used these in a while, but they fit the bill.
: Dean Michaud
|
|
|
|
|
For SHGetSpecialFolderPath() we need to give the CSIDL constants. I will not come to know which folder it is. We can even rename the My documents special folder in the desktop. So there is no way of knowing it is my documents or something else.
Regarding the SHGetFolderPath(), it wont accept the virtual folder. It works only for the real folder.
|
|
|
|