|
![](/i/fill.gif) |
On 9/29/2012 14:17, Orchid Win7 v1 wrote:
> In short, writing a correct destructor is a nightmare.
Not really. Writing a complicated class that also has a destructor is a
nightmare. If you have a destructor on a class that has anything more than a
reference to the object you want to destruct, you're probably doing it
wrong. If you're doing more than wrapping the non-GCed resource, you're
doing too much work in that class.
That said, no, destructors aren't especially useful except for logging that
you you forgot to explicitly free your resource. :-)
>> In C++ destructors are useful for more than just freeing memory. For
>> example they are commonly used in things like locks:
Personally, I find that sort of thing terribly confusing. I suppose you get
used to having variable declarations actually being control flow statements,
but I find it rather bizarre.
>> It's handy because you don't need to any special code to
>> make sure that the destructor is called when the function is exited.
I prefer the Sing# method, wherein if you have a variable that needs to be
tracked, and you let it go out of scope without disposing it, the code
doesn't compile. You can still get resource leaks with the C++ method, if
your structures are complicated enough.
--
Darren New, San Diego CA, USA (PST)
"They're the 1-800-#-GORILA of the telecom business."
Post a reply to this message
|
![](/i/fill.gif) |