CTypedPtrArray Class
Provides a type-safe "wrapper" for objects of class CPtrArray
or CObArray
.
template<class BASE_CLASS, class TYPE>
class CTypedPtrArray : public BASE_CLASS
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.
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. |
Name | Description |
---|---|
CTypedPtrArray::operator [ ] | Sets or gets the element at the specified index. |
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.
BASE_CLASS
CTypedPtrArray
Header: afxtempl.h
This member function calls BASE_CLASS
::Add.
INT_PTR Add(TYPE newElement);
TYPE
Template parameter specifying the type of element to be added to the array.
newElement
The element to be added to this array.
The index of the added element.
For more detailed remarks, see CObArray::Add.
This member function calls BASE_CLASS
::Append**.
INT_PTR Append(const CTypedPtrArray<BASE_CLASS, TYPE>& src);
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.
The index of the first appended element.
For more detailed remarks, see CObArray::Append.
This member function calls BASE_CLASS
::Copy.
void Copy(const CTypedPtrArray<BASE_CLASS, TYPE>& src);
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.
For more detailed remarks, see CObArray::Copy.
This inline function calls BASE_CLASS
::ElementAt.
TYPE& ElementAt(INT_PTR nIndex);
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.
A temporary reference to the element at the location specified by nIndex. This element is of the type specified by the template parameter TYPE.
For more detailed remarks, see CObArray::ElementAt.
This inline function calls BASE_CLASS
::GetAt.
TYPE GetAt(INT_PTR nIndex) const;
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.
A copy of the element at the location specified by nIndex. This element is of the type specified by the template parameter TYPE.
For more detailed remarks, see CObArray::GetAt
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);
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.
For more detailed remarks, see CObArray::InsertAt.
These inline operators call BASE_CLASS
::operator [ ].
TYPE& operator[ ](int_ptr nindex);
TYPE operator[ ](int_ptr nindex) const;
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.
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.
This member function calls BASE_CLASS
::SetAt.
void SetAt(
INT_PTR nIndex,
TYPE ptr);
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.
For more detailed remarks, see CObArray::SetAt.
This member function calls BASE_CLASS
::SetAtGrow.
void SetAtGrow(
INT_PTR nIndex,
TYPE newElement);
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.
For more detailed remarks, see CObArray::SetAtGrow.
MFC Sample COLLECT
Hierarchy Chart
CPtrArray Class
CObArray Class