TDPL: Manual invocation of destructor

Steven Schveighoffer schveiguy at yahoo.com
Tue Aug 10 06:28:00 PDT 2010


On Tue, 10 Aug 2010 09:21:30 -0400, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> I see. I agree clear() is definitely safer in these situations. But if  
> the
> constructor gets called again automatically after clear(), that would  
> mean
> the other references could still use the object since it's in a valid  
> state
> again, right?

Not really.  Who defines 'valid state'?  You might expect it to still  
contain the data you put in it through some member function.  An object  
goes through many states while alive, and arbitrarily picking one of those  
states and declaring it 'valid' is simply bad practice.  Better to pick a  
reasonable state that *all* objects can be set to, and call it invalid.

When you clear an object, you are saying "I no longer need this data from  
here or anywhere else"  Any access to that object after calling clear is a  
program error.  All clear does vs. delete is make it so an invalid access  
to that data doesn't cause your program to explode in unpredictable ways.

-Steve


More information about the Digitalmars-d mailing list