| title | CAutoVectorPtr Class | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| ms.date | 11/04/2016 | ||||||||
| f1_keywords |
|
||||||||
| helpviewer_keywords |
|
||||||||
| ms.assetid | 0030362b-6bc4-4a47-9b5b-3c3899dceab4 |
This class represents a smart pointer object using vector new and delete operators.
Important
This class and its members cannot be used in applications that execute in the Windows Runtime.
template<typename T>
class CAutoVectorPtr
T
The pointer type.
| Name | Description |
|---|---|
| CAutoVectorPtr::CAutoVectorPtr | The constructor. |
| CAutoVectorPtr::~CAutoVectorPtr | The destructor. |
| Name | Description |
|---|---|
| CAutoVectorPtr::Allocate | Call this method to allocate the memory required by the array of objects pointed to by CAutoVectorPtr. |
| CAutoVectorPtr::Attach | Call this method to take ownership of an existing pointer. |
| CAutoVectorPtr::Detach | Call this method to release ownership of a pointer. |
| CAutoVectorPtr::Free | Call this method to delete an object pointed to by a CAutoVectorPtr. |
| Name | Description |
|---|---|
| CAutoVectorPtr::operator T * | The cast operator. |
| CAutoVectorPtr::operator = | The assignment operator. |
| Name | Description |
|---|---|
| CAutoVectorPtr::m_p | 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. CAutoVectorPtr is similar to CAutoPtr, the only difference being that CAutoVectorPtr uses vector new[] and vector delete[] to allocate and free memory instead of the C++ new and delete operators. See CAutoVectorPtrElementTraits if collection classes of CAutoVectorPtr are required.
See CAutoPtr for an example of using a smart pointer class.
Header: atlbase.h
Call this method to allocate the memory required by the array of objects pointed to by CAutoVectorPtr.
bool Allocate(size_t nElements) throw();
nElements
The number of elements in the array.
Returns true if the memory is successfully allocated, false on failure.
In debug builds, an assertion failure will occur if the CAutoVectorPtr::m_p member variable currently points to an existing value; that is, it is not equal to NULL.
Call this method to take ownership of an existing pointer.
void Attach(T* p) throw();p
The CAutoVectorPtr object will take ownership of this pointer.
When a CAutoVectorPtr object takes ownership of a pointer, it will automatically delete the pointer and any allocated data when it goes out of scope. If CAutoVectorPtr::Detach is called, the programmer is again given responsibility for freeing any allocated resources.
In debug builds, an assertion failure will occur if the CAutoVectorPtr::m_p member variable currently points to an existing value; that is, it is not equal to NULL.
The constructor.
CAutoVectorPtr() throw();
explicit CAutoVectorPtr(T* p) throw();
CAutoVectorPtr(CAutoVectorPtr<T>& p) throw();
p
An existing pointer.
The CAutoVectorPtr object can be created using an existing pointer, in which case it transfers ownership of the pointer.
The destructor.
~CAutoVectorPtr() throw();
Frees any allocated resources. Calls CAutoVectorPtr::Free.
Call this method to release ownership of a pointer.
T* Detach() throw();
Returns a copy of the pointer.
Releases ownership of a pointer, sets the CAutoVectorPtr::m_p 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 CAutoVectorPtr object may have previously assumed responsibility.
Call this method to delete an object pointed to by a CAutoVectorPtr.
void Free() throw();The object pointed to by the CAutoVectorPtr is freed, and the CAutoVectorPtr::m_p member variable is set to NULL.
The pointer data member variable.
T* m_p;
This member variable holds the pointer information.
The assignment operator.
CAutoVectorPtr<T>& operator= (CAutoVectorPtr<T>& p) throw();
p
A pointer.
Returns a reference to a CAutoVectorPtr< T >.
The assignment operator detaches the CAutoVectorPtr object from any current pointer and attaches the new pointer, p, in its place.
The cast operator.
operator T*() const throw();
Returns a pointer to the object data type defined in the class template.