Skip to content

Latest commit

 

History

History
161 lines (100 loc) · 4.03 KB

cheapptr-class.md

File metadata and controls

161 lines (100 loc) · 4.03 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: CHeapPtr Class
CHeapPtr Class
11/04/2016
CHeapPtr
ATLCORE/ATL::CHeapPtr
ATLCORE/ATL::CHeapPtr::CHeapPtr
ATLCORE/ATL::CHeapPtr::Allocate
ATLCORE/ATL::CHeapPtr::Reallocate
CHeapPtr class
e5c5bfd4-9bf1-4164-8a83-8155fe253454

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.

Syntax

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.

Members

Public Constructors

Name Description
CHeapPtr::CHeapPtr The constructor.

Public Methods

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.

Public Operators

Name Description
CHeapPtr::operator = The assignment operator.

Remarks

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.

Inheritance Hierarchy

CHeapPtrBase

CHeapPtr

Requirements

Header: atlcore.h

CHeapPtr::Allocate

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

[!code-cppNVC_ATL_Utilities#77]

CHeapPtr::CHeapPtr

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

[!code-cppNVC_ATL_Utilities#78]

CHeapPtr::operator =

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

[!code-cppNVC_ATL_Utilities#80]

CHeapPtr::Reallocate

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

[!code-cppNVC_ATL_Utilities#79]

See also

CHeapPtrBase Class
CCRTAllocator Class
Class Overview