There is Auto_PTR in C , you can automatically allocate the DELETE, but Auto_Ptr has a shortcoming, that is, you cannot delete an array. That is, it can only call the delete, and delete can not be called []. You can design such a class from moving call delete [] to release arrays and correctly call destructor:
Template
Class arraydeletor {
Private:
T * array_;
PUBLIC:
ArrayDeletor: array_ (array) {}
T * Release () {t * RET = array_; array_ = 0; return ret;
~ Arraydeletor () {ix (array_! = 0) delete [] array_;}
}
USAGE:
T * f () // function requires returning an array of objects
{
T * a = new a [10];
ArrayDeletor deletor (a);
...
IF (error) Return 0;
...
IF (Exception) throw exception (); // does not have memory leaks
...
Return deletor.release (); // Pass OWERSHIP
}
The automatic call destructive function of the C language is a very useful feature that uses this feature to greatly reduce the leak of resources.