CAutoVectorPtr Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CAutoVectorPtr Class.

This class represents a smart pointer object using vector new and delete operators.

System_CAPS_ICON_important.jpg Important

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

template<typename T>  
class CAutoVectorPtr

Parameters

T
The pointer type.

Public Constructors

NameDescription
CAutoVectorPtr::CAutoVectorPtrThe constructor.
CAutoVectorPtr::~CAutoVectorPtrThe destructor.

Public Methods

NameDescription
CAutoVectorPtr::AllocateCall this method to allocate the memory required by the array of objects pointed to by CAutoVectorPtr.
CAutoVectorPtr::AttachCall this method to take ownership of an existing pointer.
CAutoVectorPtr::DetachCall this method to release ownership of a pointer.
CAutoVectorPtr::FreeCall this method to delete an object pointed to by a CAutoVectorPtr.

Public Operators

NameDescription
CAutoVectorPtr::operator T *The cast operator.
CAutoVectorPtr::operator =The assignment operator.

Public Data Members

NameDescription
CAutoVectorPtr::m_pThe 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();

Parameters

nElements
The number of elements in the array.

Return Value

Returns true if the memory is successfully allocated, false on failure.

Remarks

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();

Parameters

p
The CAutoVectorPtr object will take ownership of this pointer.

Remarks

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();

Parameters

p
An existing pointer.

Remarks

The CAutoVectorPtr object can be created using an existing pointer, in which case it transfers ownership of the pointer.

The destructor.

~CAutoVectorPtr() throw();

Remarks

Frees any allocated resources. Calls CAutoVectorPtr::Free.

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 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();

Remarks

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;

Remarks

This member variable holds the pointer information.

The assignment operator.

CAutoVectorPtr<T>& operator= (CAutoVectorPtr<T>& p) throw();

Parameters

p
A pointer.

Return Value

Returns a reference to a CAutoVectorPtr< T >.

Remarks

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();

Remarks

Returns a pointer to the object data type defined in the class template.

CAutoPtr Class
Class Overview

Show: