Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
CAutoPtr Class
Collapse the table of content
Expand the table of content

CAutoPtr Class

This class represents a smart pointer object.

Important note Important

This class and its members cannot be used in applications that execute in the Windows Runtime.

typename T 
class CAutoPtr


The pointer type.




The constructor.


The destructor.




Call this method to take ownership of an existing pointer.


Call this method to release ownership of a pointer.


Call this method to delete an object pointed to by a CAutoPtr.



CAutoPtr::operator T*

The cast operator.

CAutoPtr::operator =

The assignment operator.

CAutoPtr::operator ->

The pointer-to-member operator.




The pointer data member variable.

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.

Header: atlbase.h

// A simple class for demonstration purposes 

class MyClass 
   int iA;
   int iB;
   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)

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;

   // The overloaded -> operator allows the  
   // CAutoPtr object to be used in place of the pointer.

   // 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.

   // Detach breaks the association, so after this 
   // call, pMyC is controlled only by apMyC.

   // CAutoPtr destroys any object it controls when it 
   // goes out of scope, so apMyC destroys the object  
   // pointed to by pMyC here. 
   return 0;
© 2015 Microsoft