Syntax of CAutoPtr
The syntax for the CAutoPtr class is given below.
template <typename T>
class CAutoPtr

You can also try this code with Online C++ Compiler
Run Code
Parameters
The class takes only one parameter, T, which denotes the pointer type.
Members
CAutoPtr class consists of members like public constructors, destructors, methods, operators, and data members.
Let's see each of them in detail.
Public Constructors
CAutoPtr::CAutoPtr is the constructor that initializes new instances of CAutoPtr class.
Public Destructors
CAutoPtr::~CAutoPtr is the destructor that gets called when the CAutoPtr object goes out of scope. The destructor frees the resources allocated to the object by calling CAutoPtr::Free.
Public Methods
CAutoPtr provides the following public methods, which are accessible both inside and outside the scope of the class:
-
CAutoPtr::Attach
You can use the method Attach to take ownership of an existing pointer.
The prototype of the function is
void Attach(T* p) throw();

You can also try this code with Online C++ Compiler
Run Code
The function takes one parameter, p, and the CAutoPtr object takes ownership of this pointer.
-
CAutoPtr::Detach
You can use the method Detach to release ownership of a pointer. It returns a copy of the pointer and sets the pointer data member variable to NULL, thus releasing the ownership of the pointer.
The function prototype is
T* Detach() throw();

You can also try this code with Online C++ Compiler
Run Code
-
CAutoPtr::Free
It deletes an object pointed to by a CAutoPtr and sets the public data member variable CAutoPtr::m_p to NULL.
The function prototype is:
void Free() throw();

You can also try this code with Online C++ Compiler
Run Code
Public Operators
CAutoPtr supports the following operators:
-
CAutoPtr::operator T*
T* is a cast operator which returns a pointer to the object data type defined in the class template.
Operator definition:
operator T* () const throw();

You can also try this code with Online C++ Compiler
Run Code
-
CAutoPtr::operator =
The assignment operator returns a reference to a CAutoPtr< T >.
CAutoPtr provides two assignment operators.
Operator definition:
template<>
CAutoPtr<T>& operator= (CAutoPtr<T>& p);

You can also try this code with Online C++ Compiler
Run Code
template<typename TSrc>
CAutoPtr<T>& operator= (CAutoPtr<TSrc>& p);
Here, p is a pointer, and TSrc is a class type. The assignment operator detaches the CAutoPtr object from any existing pointer and attaches the new pointer, p, instead.
-
CAutoPtr::operator ->
The operator -> is a pointer-to-member operator which returns the value of the CAutoPtr::m_p data member variable.
Operator definition:
T* operator->() const throw();
You can use this operator for calling a method in a class pointed to by the CAutoPtr object.
Public Data Members
The class CAutoPtr consists of one public data member:
-
CAutoPtr::m_p
It is the pointer data member variable that stores the pointer information.
Data member definition:
T* m_p;

You can also try this code with Online C++ Compiler
Run Code
Example
Let's see an example that will demonstrate the class CAutoPtr.
#include<atlbase.h>
/* A simple class for demonstration purposes */
class MyCAutoPtrClass
{
int iA;
int iB;
public:
MyCAutoPtrClass(int a, int b);
void Test();
};
MyCAutoPtrClass::MyCAutoPtrClass(int a, int b)
{
std::cout << “Constructor called” << std::endl;
iA = a;
iB = b;
}
void MyCAutoPtrClass::Test()
{
std::cout << iA << std::endl;
std::cout << iB << std::endl;
ATLASSERT(iA == iB);
}
void MyFunction(MyCAutoPtrClass* c)
{
c->Test();
}
int UseMyCAutoPtrClass()
{
// Creating a MyCAutoPtrClass object
MyCAutoPtrClass *myObj = new MyCAutoPtrClass(1, 1);
CAutoPtr<MyCAutoPtrClass> amyObj;
/* The CAutoPtr object amyObj takes over the myObj pointer using Attach */
amyObj.Attach(myObj);
// CAutoPtr object is now used in place of the pointer using the -> operator
amyObj->Test();
// using assignment operator
CAutoPtr<MyCAutoPtrClass> amyObj2;
amyObj2 = amyObj;
// using casting operator
MyFunction(myObj);
MyFunction(amyObj2);
//using detach, now only amyObj2 controls the amyObj
amyObj2.Detach();
//destructor is called when the CAutoPtr object goes out of scope
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Importance of CAutoPtr
The important features of CAutoPtr are as follows:
-
It helps to create and manage smart pointers.
-
It helps to prevent memory leaks.
-
It reduces the chances of repetitive deletion of the same pointer because it does not allow the storage of the same pointer in two different CAutoPtr objects.
This becomes possible through the use of copy constructor and assignment operator.
- It makes it easy to create collections of pointers. It's simple to create a collection of CAutoPtr objects, and the objects themselves take care of the release of memory when you delete the collection.
Frequently Asked Questions
How are smart pointers implemented?
In C++, a smart pointer is implemented as a template class that mimics, utilizing operator overloading, the behaviors of a traditional (raw) pointer while providing additional memory management features.
What is the difference between CAutoVectorPtr and CAutoPtr?
CAutoVectorPtr is similar to CAutoPtr, the only difference being that it uses vector new[] and vector delete[] to allocate and free memory.
How many types of smart pointers are there?
C++ has three types of Smart Pointers which include Unique_ptr, Shared_ptr, and Weak_ptr.
Conclusion
In this article, we learned about the memory management class CAutoPtr, its syntax, parameters, members, constructors, etc., along with some examples to get a practical understanding.
We hope this blog has helped you enhance your knowledge of the memory management class CAutoPtr.
Check out these useful blogs on ATL and CAutoPtr -
🎯 ATL module classes
🎯 Memory management class-CAutoPtrArray and CAutoPtrList
🎯 Memory management class-CComGITPtr
Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, interview puzzles, take a look at the interview experiences, and interview bundle for placement preparations.
Do upvote our blog to help other ninjas grow🌱
Happy Reading!!💻