CHeapPtr Class

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see CHeapPtr Class on docs.microsoft.com. A smart pointer class for managing heap pointers.

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 Allocator=CCRTAllocator>  
class CHeapPtr : public CHeapPtrBase<T, Allocator>

Parameters

T
The object type to be stored on the heap.

Allocator
The memory allocation class to use.

Public Constructors

NameDescription
CHeapPtr::CHeapPtrThe constructor.

Public Methods

NameDescription
CHeapPtr::AllocateCall this method to allocate memory on the heap to store objects.
CHeapPtr::ReallocateCall this method to reallocate the memory on the heap.

Public Operators

NameDescription
CHeapPtr::operator =The assignment operator.

CHeapPtr is derived from CHeapPtrBase and by default uses the CRT routines (in CCRTAllocator) to allocate and free memory. The class CHeapPtrList may be used to construct a list of heap pointers. See also CComHeapPtr, which uses COM memory allocation routines.

CHeapPtrBase

CHeapPtr

Header: atlcore.h

Call this method to allocate memory on the heap to store objects.

bool Allocate(size_t nElements = 1) throw();

Parameters

nElements
The number of elements used to calculate the amount of memory to allocate. The default value is 1.

Return Value

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

Remarks

The allocator routines are used to reserve enough memory on the heap to store nElement objects of a type defined in the constructor.

Example

   // Create a new CHeapPtr object
   CHeapPtr <int> myHP;
   // Allocate space for 10 integers on the heap
   myHP.Allocate(10);

The constructor.

CHeapPtr() throw();
explicit CHeapPtr(T* p) throw();
CHeapPtr(CHeapPtr<T, Allocator>& p) throw();

Parameters

p
An existing heap pointer or CHeapPtr.

Remarks

The heap pointer can optionally be created using an existing pointer, or a CHeapPtr object. If so, the new CHeapPtr object assumes responsibility for managing the new pointer and resources.

Example

   // Create a new CHeapPtr object
   CHeapPtr <int> myHP;
   // Create a new CHeapPtr from the first
   CHeapPtr <int> myHP2(myHP);   

Assignment operator.

CHeapPtr<T, Allocator>& operator=(
    CHeapPtr<T, Allocator>& p) throw();

Parameters

p
An existing CHeapPtr object.

Return Value

Returns a reference to the updated CHeapPtr.

Example

   // Create a new CHeapPtr object
   CHeapPtr <int> myHP;
   // Allocate space for 10 integers on the heap
   myHP.Allocate(10);
   // Create a second heap pointer
   // and assign it to the first pointer.
   CHeapPtr <int> myHP2;
   myHP2 = myHP;   

Call this method to reallocate the memory on the heap.

bool Reallocate(size_t nElements) throw();

Parameters

nElements
The new number of elements used to calculate the amount of memory to allocate.

Return Value

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

Example

   // Create a new CHeapPtr object
   CHeapPtr <int> myHP;
   // Allocate space for 10 integers on the heap
   myHP.Allocate(10);
   // Resize the allocated memory for 20 integers
   myHP.Reallocate(20);   

CHeapPtrBase Class
CCRTAllocator Class
Class Overview

Show: