TDPL: Manual invocation of destructor

Michel Fortin michel.fortin at michelf.com
Mon Aug 9 05:48:42 PDT 2010


On 2010-08-09 08:28:38 -0400, Andrej Mitrovic 
<andrej.mitrovich at gmail.com> said:

> "After you invoke clear, the object is still alive and well, but its
> destructor has been called and the object is now carrying its
> default-constructed stated. During the next garbage collection, the
> destructor is called again, because the garbage collector has no idea in
> what state you have left the object."
> 
> But in what situation would you want to manipulate an object that was
> already cleared and ready for garbage collection?

To me, 'clear' looks like a way to implement a 'removeAll' function on 
containers but at a lower level that kind of work anywhere. This has 
the benefit that you don't have to implement clear on every container. 
But in the general case I find it rather pointless, and quite dangerous 
too as it can break some invariants, not invariant in the class itself 
but in the program as a whole.

The more I think about it, the more it looks like a misfeature in the 
same league as copy constructors that everyone has to disable in C++. 
But is there any way to disable 'clear' for a given class? I guess not, 
since all classes can be casted back to Object. Though perhaps omitting 
the default constructor would work.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list