On 2010-08-09 08:28:38 -0400, Andrej Mitrovic <[email protected]> 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
[email protected]
http://michelf.com/

Reply via email to