AutoDeleter

 

Derived from: none

Declared in: AutoDeleter.h

Library: none


Overview

If you new temporary objects in a function with several points of exit, at each of which the temporary objects need to be deleted, it clutters your code. If you instead create an AutoDeleter on the stack, your dynamically allocated temporary objects will be deleted with cleaner code. Also, if you use exceptions, as the stack is unwound until the exception is caught, only stack variables will be deleted, not dynamically allocated ones. If you create an AutoDeleter for dynamically allocated objects, they will be deleted where necessary as the stack is unwound. Note that the object will also be deleted if the AutoDeleter goes out of scope.

 


Constructor and Destructor


AutoDeleter(), AutoDeleterArray()

AutoDeleter(list_content_type content) AutoDeleterArray(list_content_type content)

Creates an AutoDeleter. It should actually be used as follows:

char* must_delete = new char[10];
AutoDeleterArray<char*> deleter(must_delete);

SomeObject* must_delete_too = new SomeObject();
AutoDeleter<SomeObject*> deleter(must_delete_too);

It is safe to construct an AutoDeleter or AutoDeleterArray passing in a NULL argument.


~AutoDeleter(), ~AutoDeleterArray()

~AutoDeleter()
~AutoDeleterArray()

Destroys the object or array it is charged with deleting.


Member Functions

 


DoNotDelete()

void DoNotDelete()

If you need to delete the object which the AutoDeleter is charged with deleting before the AutoDeleter has gone out of scope or the function has exited, or need to keep the object after all, perhaps to return it from the function, you can call this method to clear the AutoDeleter's record of the object.


By Brian Tietz

Copyright 2000

Bug reports (including documentation errors) and feature requests can be sent to briant@timelinevista.com.