Suicidal objects
Sean Kelly
sean at f4.ca
Mon Dec 10 08:29:15 PST 2007
Jarrett Billingsley wrote:
> "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..
This thread may be relevant:
http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html
In short, there's no easy way to perform placement new on an existing
class, and I view this as a deficiency. My proposal would provide one
easy way to do so, but I still feel that making it automatic would be
preferable.
Sean
More information about the Digitalmars-d-learn
mailing list