SimpleLib

Auto (Smart) Pointer Class

CAutoPtr is a simple automatic or smart pointer class supporting the same semantic behaviours as SimpleLib's container classes. It therefore supports owned pointers, reference counted pointers and custom semantics classes.

template <class T, class TSem=SOwnedPtr>
class CAutoPtr;

To declare a pointer to an object that will be automatically deleted, use the CAutoPointer with no second argument:

CAutoPtr<CMyObject> spObject;

Any pointer assigned to spObject will now be automatically deleted when spObject goes out of scope.

Similarly to the SimpleLib's container classes, CAutoPtr also supports detaching a pointer without deleting it:

return spObject.Detach();

For reference counted objects such as COM interface pointers, CAutoPtr can be used to automatically release an object:

CAutoPtr<IUnknown, SRefCounted> spObject;

CAutoPtr supports a flexible set of operators that allow it to be treated as a regular pointer variable in most cases.