destructors

Daniel Giddings danielg at microforte.com
Tue May 1 17:24:05 PDT 2007


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


More information about the Digitalmars-d-learn mailing list