[Home] [Prev] [Next] [Up]
autoptr template
Provides a simple template to manipulate a pointer, and allows for deleting the pointer when the stack is unwound.
Usage:
#include <XAutoPtr.h>
template autoptr<class T>;
Description
This provides similar functionality to the auto_ptr template.
Normally you would create a pointer using this template by writing:
autoptr<class T> tptr;For the most part, you can then manipulate the tptr variable as if it were a pointer to class T. That is, you can copy, extract fields from it, and otherwise play with the pointer:
tptr = new T; tptr->TMethod();Additional utilities are provided to assist in manipulating the autoptr<> object. When the stack is unwound (such as when returning from the function or when an exception is thrown), the object the autoptr<> template points to is automatically released.
Notes
The autoptr class does not currently work with fundamental types, such as int, float, or double.
Construction/Destruction Methods
autoptr::autoptr(T *ptr = NULL)
Construct this pointer. By default the pointer is initialized to NULL.
autoptr::~autoptr
Delete the pointer associated with this object.
Pointer access
T * autoptr::operator ->(void)
This returns a pointer to the stored object. This allows the tptr->x construct to be written.
T& autoptr::operator *(void)
This returns a reference to the stored object. This allows the *tptr construct to be written.
int autoptr::operator !(void)
This returns non-zero if the stored pointer is not NULL. This allows the expression !tptr to be written.
Pointer manipulation utilities
void autoptr::Detach(void)
This sets the contents of the pointer to NULL. If the pointer is copied to another location, this allows the pointer to be released from this autoptr<> template.
T* autoptr:Return(void)
This returns a pointer, and detaches it.
T* autoptr:Ptr(void)
This returns a pointer stored in this construct.