CHeapPtr Class
A smart pointer class for managing heap pointers.
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>
T
The object type to be stored on the heap.
Allocator
The memory allocation class to use.
Name | Description |
---|---|
CHeapPtr::CHeapPtr | The constructor. |
Name | Description |
---|---|
CHeapPtr::Allocate | Call this method to allocate memory on the heap to store objects. |
CHeapPtr::Reallocate | Call this method to reallocate the memory on the heap. |
Name | Description |
---|---|
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.
CHeapPtr
Header: atlcore.h
Call this method to allocate memory on the heap to store objects.
bool Allocate(size_t nElements = 1) throw();
nElements
The number of elements used to calculate the amount of memory to allocate. The default value is 1.
Returns true if the memory was successfully allocated, false on failure.
The allocator routines are used to reserve enough memory on the heap to store nElement objects of a type defined in the constructor.
// 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();
p
An existing heap pointer or CHeapPtr
.
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.
// 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();
p
An existing CHeapPtr
object.
Returns a reference to the updated CHeapPtr
.
// 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();
nElements
The new number of elements used to calculate the amount of memory to allocate.
Returns true if the memory was successfully allocated, false on failure.
// 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);