Suicidal objects

Jarrett Billingsley kb3ctd2 at yahoo.com
Mon Dec 10 06:14:36 PST 2007


"Mike" <vertex at gmx.at> wrote in message news:op.t224pyqokgfkbn at lucia...
> Out of curiosity I decided to try out how objects could delete themselves 
> and what happened if they did. I found out that "delete this;" actually 
> works.
>
> AFAIK there's no placement new in D, which could be a reason for allowing 
> this behavior. Other than that, any attempts to delete or assign to "this" 
> should be illegal inside methods. It's a minor thing - I don't think 
> "delete this;" is a common typo and any sane programmer wouldn't do that 
> on purpose.
>
> So: what do you think? Are there any useful applications for 
> assigning/deleting "this"?

There _is_ actually placement new in D, it just isn't provided by default 
like it is in C++.  You can overload the 'new' operator on a per-class 
basis, and writing a placement new is as easy as returning the pointer 
passed into 'new'.

Furthermore, scoped class references allocated with 'new' like:

scope a = new A();

are actually allocated on the stack.

In both cases, 'delete this' seems bogus.. 




More information about the Digitalmars-d-learn mailing list