CAutoPtr Class
The latest version of this topic can be found at CAutoPtr Class.
This class represents a smart pointer object.
Important
This class and its members cannot be used in applications that execute in the Windows Runtime.
Syntax
template <typename T>
class CAutoPtr
Parameters
T
The pointer type.
Members
Public Constructors
Name | Description |
---|---|
CAutoPtr::CAutoPtr | The constructor. |
CAutoPtr::~CAutoPtr | The destructor. |
Public Methods
Name | Description |
---|---|
CAutoPtr::Attach | Call this method to take ownership of an existing pointer. |
CAutoPtr::Detach | Call this method to release ownership of a pointer. |
CAutoPtr::Free | Call this method to delete an object pointed to by a CAutoPtr . |
Public Operators
Name | Description |
---|---|
CAutoPtr::operator T* | The cast operator. |
CAutoPtr::operator = | The assignment operator. |
CAutoPtr::operator -> | The pointer-to-member operator. |
Public Data Members
Name | Description |
---|---|
CAutoPtr::m_p | The pointer data member variable. |
Remarks
This class provides methods for creating and managing a smart pointer, which will help protect against memory leaks by automatically freeing resources when it falls out of scope.
Further, CAutoPtr
's copy constructor and assignment operator transfer ownership of the pointer, copying the source pointer to the destination pointer and setting the source pointer to NULL. It is therefore impossible to have two CAutoPtr
objects each storing the same pointer, and this reduces the possibility of deleting the same pointer twice.
CAutoPtr
also simplifies the creation of collections of pointers. Instead of deriving a collection class and overriding the destructor, it's simpler to make a collection of CAutoPtr
objects. When the collection is deleted, the CAutoPtr
objects will go out of scope and automatically delete themselves.
CHeapPtr and variants work in the same way as CAutoPtr
, except that they allocate and free memory using different heap functions instead of the C++ new and delete operators. CAutoVectorPtr is similar to CAutoPtr
, the only difference being that it uses vector new[] and vector delete[] to allocate and free memory.
See also CAutoPtrArray and CAutoPtrList when arrays or lists of smart pointers are required.
Requirements
Header: atlbase.h
Example
// A simple class for demonstration purposes
class MyClass
{
int iA;
int iB;
public:
MyClass(int a, int b);
void Test();
};
MyClass::MyClass(int a, int b)
{
iA = a;
iB = b;
}
void MyClass::Test()
{
ATLASSERT(iA == iB);
}
// A simple function
void MyFunction(MyClass* c)
{
c->Test();
}
int UseMyClass()
{
// Create an object of MyClass.
MyClass *pMyC = new MyClass(1, 1);
// Create a CAutoPtr object and have it take
// over the pMyC pointer by calling Attach.
CAutoPtr<MyClass> apMyC;
apMyC.Attach(pMyC);
// The overloaded -> operator allows the
// CAutoPtr object to be used in place of the pointer.
apMyC->Test();
// Assign a second CAutoPtr, using the = operator.
CAutoPtr<MyClass> apMyC2;
apMyC2 = apMyC;
// The casting operator allows the
// object to be used in place of the pointer.
MyFunction(pMyC);
MyFunction(apMyC2);
// Detach breaks the association, so after this
// call, pMyC is controlled only by apMyC.
apMyC2.Detach();
// CAutoPtr destroys any object it controls when it
// goes out of scope, so apMyC destroys the object
// pointed to by pMyC here.
return 0;
}
CAutoPtr::Attach
Call this method to take ownership of an existing pointer.
void Attach(T* p) throw();
Parameters
p
The CAutoPtr
object will take ownership of this pointer.
Remarks
When a CAutoPtr
object takes ownership of a pointer, it will automatically delete the pointer and any allocated data when it goes out of scope. If CAutoPtr::Detach is called, the programmer is again given responsibility for freeing any allocated resources.
In debug builds, an assertion failure will occur if the CAutoPtr::m_p data member currently points to an existing value; that is, it is not equal to NULL.
Example
See the example in the CAutoPtr Overview.
CAutoPtr::CAutoPtr
The constructor.
CAutoPtr() throw();
explicit CAutoPtr(T* p) throw();
template<typename TSrc>
CAutoPtr(CAutoPtr<TSrc>& p) throw();
template<>
CAutoPtr(CAutoPtr<T>& p) throw();
Parameters
p
An existing pointer.
TSrc
The type being managed by another CAutoPtr
, used to initialize the current object.
Remarks
The CAutoPtr
object can be created using an existing pointer, in which case it transfers ownership of the pointer.
Example
See the example in the CAutoPtr Overview.
CAutoPtr::~CAutoPtr
The destructor.
~CAutoPtr() throw();
Remarks
Frees any allocated resources. Calls CAutoPtr::Free.
CAutoPtr::Detach
Call this method to release ownership of a pointer.
T* Detach() throw();
Return Value
Returns a copy of the pointer.
Remarks
Releases ownership of a pointer, sets the CAutoPtr::m_p data member variable to NULL, and returns a copy of the pointer. After calling Detach, it is up to the programmer to free any allocated resources over which the CAutoPtr
object may have previously assumed reponsibility.
Example
See the example in the CAutoPtr Overview.
CAutoPtr::Free
Call this method to delete an object pointed to by a CAutoPtr
.
void Free() throw();
Remarks
The object pointed to by the CAutoPtr
is freed, and the CAutoPtr::m_p data member variable is set to NULL.
CAutoPtr::m_p
The pointer data member variable.
T* m_p;
Remarks
This member variable holds the pointer information.
CAutoPtr::operator =
The assignment operator.
template<>
CAutoPtr<T>& operator= (CAutoPtr<T>& p);
template<typename TSrc>
CAutoPtr<T>& operator= (CAutoPtr<TSrc>& p);
Parameters
p
A pointer.
TSrc
A class type.
Return Value
Returns a reference to a CAutoPtr< T >.
Remarks
The assignment operator detaches the CAutoPtr
object from any current pointer and attaches the new pointer, p
, in its place.
Example
See the example in the CAutoPtr Overview.
CAutoPtr::operator ->
The pointer-to-member operator.
T* operator->() const throw();
Return Value
Returns the value of the CAutoPtr::m_p data member variable.
Remarks
Use this operator to call a method in a class pointed to by the CAutoPtr
object. In debug builds, an assertion failure will occur if the CAutoPtr
points to NULL.
Example
See the example in the CAutoPtr Overview.
CAutoPtr::operator T*
The cast operator.
operator T*() const throw();
Return Value
Returns a pointer to the object data type defined in the class template.
Example
See the example in the CAutoPtr Overview.