Newbie initial comments on D language - destructor usage

Janice Caron caron800 at googlemail.com
Wed Jan 30 00:32:46 PST 2008


On Jan 30, 2008 4:43 AM, James Dennett <jdennett at acm.org> wrote:
> I suspect Janice just mis-quoted some C++ code, moving "delete this"
> to somewhere it doesn't belong.

No wait - actually you're right.
Yes, I had moved "delete this" to the wrong place. It actually goes in
a member function, but not the destructor. From memory, the example
was a Thread class, and it called delete on return from the virtual
run() function, so that the threads resources got cleaned up after the
thread had terminated. So, thanks for the correction.

Nonetheless, it is possible (and in some cases legitimate) to call the
destructor code without the deallocator, or vice versa.

To destruct then deallocate (the normal situation)
    delete p;

To destruct without dealocating:
    p->~Whatever();
(where Whatever is typeof(*p))

To deallocate without destructing:
   operator delete(p);

Whether or not this is useful is another question.

I don't know if the same distinctions can be made in D (or even
whether or not we'd want them). In any case, it's all very off-topic
with regard to the original question, so my apologies for the
digression. And now back to our regularly scheduled discussion...



More information about the Digitalmars-d mailing list