共用方式為


CTypedPtrArray 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 CTypedPtrArray Class.

Provides a type-safe "wrapper" for objects of class CPtrArray or CObArray.

Syntax

template<class BASE_CLASS, class TYPE>  
class CTypedPtrArray : public BASE_CLASS  

Parameters

BASE_CLASS
Base class of the typed pointer array class; must be an array class ( CObArray or CPtrArray).

TYPE
Type of the elements stored in the base-class array.

Members

Public Methods

Name Description
CTypedPtrArray::Add Adds a new element to the end of an array. Grows the array if necessary
CTypedPtrArray::Append Adds the contents of one array to the end of another. Grows the array if necessary
CTypedPtrArray::Copy Copies another array to the array; grows the array if necessary.
CTypedPtrArray::ElementAt Returns a temporary reference to the element pointer within the array.
CTypedPtrArray::GetAt Returns the value at a given index.
CTypedPtrArray::InsertAt Inserts an element (or all the elements in another array) at a specified index.
CTypedPtrArray::SetAt Sets the value for a given index; array not allowed to grow.
CTypedPtrArray::SetAtGrow Sets the value for a given index; grows the array if necessary.

Public Operators

Name Description
CTypedPtrArray::operator [ ] Sets or gets the element at the specified index.

Remarks

When you use CTypedPtrArray rather than CPtrArray or CObArray, the C++ type-checking facility helps eliminate errors caused by mismatched pointer types.

In addition, the CTypedPtrArray wrapper performs much of the casting that would be required if you used CObArray or CPtrArray.

Because all CTypedPtrArray functions are inline, use of this template does not significantly affect the size or speed of your code.

For more information on using CTypedPtrArray, see the articles Collections and Template-Based Classes.

Inheritance Hierarchy

BASE_CLASS

CTypedPtrArray

Requirements

Header: afxtempl.h

CTypedPtrArray::Add

This member function calls BASE_CLASS::Add.

INT_PTR Add(TYPE newElement);

Parameters

TYPE
Template parameter specifying the type of element to be added to the array.

newElement
The element to be added to this array.

Return Value

The index of the added element.

Remarks

For more detailed remarks, see CObArray::Add.

CTypedPtrArray::Append

This member function calls BASE_CLASS::Append.

INT_PTR Append(const CTypedPtrArray<BASE_CLASS, TYPE>& src);

Parameters

BASE_CLASS
Base class of the typed pointer array class; must be an array class ( CObArray or CPtrArray).

TYPE
Type of the elements stored in the base-class array.

src
Source of the elements to be appended to an array.

Return Value

The index of the first appended element.

Remarks

For more detailed remarks, see CObArray::Append.

CTypedPtrArray::Copy

This member function calls BASE_CLASS::Copy.

void Copy(const CTypedPtrArray<BASE_CLASS, TYPE>& src);

Parameters

BASE_CLASS
Base class of the typed pointer array class; must be an array class ( CObArray or CPtrArray).

TYPE
Type of the elements stored in the base-class array.

src
Source of the elements to be copied to an array.

Remarks

For more detailed remarks, see CObArray::Copy.

CTypedPtrArray::ElementAt

This inline function calls BASE_CLASS::ElementAt.

TYPE& ElementAt(INT_PTR nIndex);

Parameters

TYPE
Template parameter specifying the type of elements stored in this array.

nIndex
An integer index that is greater than or equal to 0 and less than or equal to the value returned by BASE_CLASS::GetUpperBound.

Return Value

A temporary reference to the element at the location specified by nIndex. This element is of the type specified by the template parameter TYPE.

Remarks

For more detailed remarks, see CObArray::ElementAt.

CTypedPtrArray::GetAt

This inline function calls BASE_CLASS::GetAt.

TYPE GetAt(INT_PTR nIndex) const;  

Parameters

TYPE
Template parameter specifying the type of elements stored in the array.

nIndex
An integer index that is greater than or equal to 0 and less than or equal to the value returned by BASE_CLASS::GetUpperBound.

Return Value

A copy of the element at the location specified by nIndex. This element is of the type specified by the template parameter TYPE.

Remarks

For more detailed remarks, see CObArray::GetAt

CTypedPtrArray::InsertAt

This member function calls BASE_CLASS::InsertAt.

void InsertAt(
    INT_PTR nIndex,  
    TYPE newElement,  
    INT_PTR nCount = 1);

 
void InsertAt(
    INT_PTR nStartIndex,  
    CTypedPtrArray<BASE_CLASS, TYPE>* pNewArray);

Parameters

nIndex
An integer index that may be greater than the value returned by CObArray::GetUpperBound.

TYPE
Type of the elements stored in the base-class array.

newElement
The object pointer to be placed in this array. A newElement of value NULL is allowed.

nCount
The number of times this element should be inserted (defaults to 1).

nStartIndex
An integer index that may be greater than the value returned by CObArray::GetUpperBound.

BASE_CLASS
Base class of the typed pointer array class; must be an array class ( CObArray or CPtrArray).

pNewArray
Another array that contains elements to be added to this array.

Remarks

For more detailed remarks, see CObArray::InsertAt.

CTypedPtrArray::operator [ ]

These inline operators call BASE_CLASS::operator [ ].

TYPE& operator[](INT_PTR nIndex);

 
TYPE operator[](INT_PTR nIndex) const;

 

Parameters

TYPE
Template parameter specifying the type of elements stored in the array.

nIndex
An integer index that is greater than or equal to 0 and less than or equal to the value returned by BASE_CLASS::GetUpperBound.

Remarks

The first operator, called for arrays that are not const, can be used on either the right (r-value) or the left (l-value) of an assignment statement. The second, invoked for const arrays, can be used only on the right.

The Debug version of the library asserts if the subscript (either on the left or right side of an assignment statement) is out of bounds.

CTypedPtrArray::SetAt

This member function calls BASE_CLASS::SetAt.

void SetAt(
    INT_PTR nIndex,  
    TYPE ptr);

Parameters

nIndex
An integer index that is greater than or equal to 0 and less than or equal to the value returned by CObArray::GetUpperBound.

TYPE
Type of the elements stored in the base-class array.

ptr
A pointer to the element to be inserted in the array at the nIndex. A NULL value is allowed.

Remarks

For more detailed remarks, see CObArray::SetAt.

CTypedPtrArray::SetAtGrow

This member function calls BASE_CLASS::SetAtGrow.

void SetAtGrow(
    INT_PTR nIndex,  
    TYPE newElement);

Parameters

nIndex
An integer index that is greater than or equal to 0.

TYPE
Type of the elements stored in the base-class array.

newElement
The object pointer to be added to this array. A NULL value is allowed.

Remarks

For more detailed remarks, see CObArray::SetAtGrow.

See Also

MFC Sample COLLECT
Hierarchy Chart
CPtrArray Class
CObArray Class