destructors
Daniel Keep
daniel.keep.lists at gmail.com
Tue May 1 20:15:30 PDT 2007
Daniel Giddings wrote:
> Bill Baxter wrote:
>>
>> I was thinking that too. That might be ok for some use cases, but it
>> doesn't help with 'scope' instances (the destructor will get called
>> not your 'destroy()' method). And in generic cases, like a tuple of
>> objects you want to destroy, the generic function has no way of
>> knowing that it should call 'obj.destroy()' on some objects instead of
>> 'delete obj'.
>>
>>
>> --bb
>
> With a small change you could catch those cases, but it is starting to
> get a bit messy.
>
> class MyClass
> {
> OtherClass a;
> bool destroyed = false;
> this() { a = new OtherClass; }
> ~this() { if( !destroyed ) throw new Exception( "~this without
> ..destroy" ); }
> MyClass destroy() { delete a; destroyed = true; delete this; return
> null; }
> }
>
> :-) Dan
I thought exceptions thrown in a destructor got ignored?
-- Daniel
--
int getRandomNumber()
{
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
http://xkcd.com/
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d-learn
mailing list