Skip to content

Latest commit

 

History

History
809 lines (534 loc) · 34 KB

cobarray-class.md

File metadata and controls

809 lines (534 loc) · 34 KB
titledescriptionms.datef1_keywordshelpviewer_keywords
CObArray Class
API reference for the `CObArray` `MFC` class which stores `CObject` pointers in an array.
08/27/2020
CObArray
AFXCOLL/CObArray
AFXCOLL/CObArray::CObArray
AFXCOLL/CObArray::Add
AFXCOLL/CObArray::Append
AFXCOLL/CObArray::Copy
AFXCOLL/CObArray::ElementAt
AFXCOLL/CObArray::FreeExtra
AFXCOLL/CObArray::GetAt
AFXCOLL/CObArray::GetCount
AFXCOLL/CObArray::GetData
AFXCOLL/CObArray::GetSize
AFXCOLL/CObArray::GetUpperBound
AFXCOLL/CObArray::InsertAt
AFXCOLL/CObArray::IsEmpty
AFXCOLL/CObArray::RemoveAll
AFXCOLL/CObArray::RemoveAt
AFXCOLL/CObArray::SetAt
AFXCOLL/CObArray::SetAtGrow
AFXCOLL/CObArray::SetSize
CObArray [MFC], CObArray
CObArray [MFC], Add
CObArray [MFC], Append
CObArray [MFC], Copy
CObArray [MFC], ElementAt
CObArray [MFC], FreeExtra
CObArray [MFC], GetAt
CObArray [MFC], GetCount
CObArray [MFC], GetData
CObArray [MFC], GetSize
CObArray [MFC], GetUpperBound
CObArray [MFC], InsertAt
CObArray [MFC], IsEmpty
CObArray [MFC], RemoveAll
CObArray [MFC], RemoveAt
CObArray [MFC], SetAt
CObArray [MFC], SetAtGrow
CObArray [MFC], SetSize

CObArray Class

Supports arrays of CObject pointers.

Syntax

classCObArray : publicCObject

Members

Public Constructors

NameDescription
CObArray::CObArrayConstructs an empty array for CObject pointers.

Public Methods

NameDescription
CObArray::AddAdds an element to the end of the array; grows the array if necessary.
CObArray::AppendAppends another array to the array; grows the array if necessary.
CObArray::CopyCopies another array to the array; grows the array if necessary.
CObArray::ElementAtReturns a temporary reference to the element pointer within the array.
CObArray::FreeExtraFrees all unused memory above the current upper bound.
CObArray::GetAtReturns the value at a given index.
CObArray::GetCountGets the number of elements in this array.
CObArray::GetDataAllows access to elements in the array. Can be NULL.
CObArray::GetSizeGets the number of elements in this array.
CObArray::GetUpperBoundReturns the largest valid index.
CObArray::InsertAtInserts an element (or all the elements in another array) at a specified index.
CObArray::IsEmptyDetermines if the array is empty.
CObArray::RemoveAllRemoves all the elements from this array.
CObArray::RemoveAtRemoves an element at a specific index.
CObArray::SetAtSets the value for a given index; array not allowed to grow.
CObArray::SetAtGrowSets the value for a given index; grows the array if necessary.
CObArray::SetSizeSets the number of elements to be contained in this array.

Public Operators

NameDescription
CObArray::operator []Sets or gets the element at the specified index.

Remarks

These object arrays are similar to C arrays, but they can dynamically shrink and grow as necessary.

Array indexes always start at position 0. You can decide whether to fix the upper bound or allow the array to expand when you add elements past the current bound. Memory is allocated contiguously to the upper bound, even if some elements are NULL.

Under Win32, the size of a CObArray object is limited only to available memory.

As with a C array, the access time for a CObArray indexed element is constant and is independent of the array size.

CObArray incorporates the IMPLEMENT_SERIAL macro to support serialization and dumping of its elements. If an array of CObject pointers is stored to an archive, either with the overloaded insertion operator or with the Serialize member function, each CObject element is, in turn, serialized along with its array index.

If you need a dump of individual CObject elements in an array, you must set the depth of the CDumpContext object to 1 or greater.

When a CObArray object is deleted, or when its elements are removed, only the CObject pointers are removed, not the objects they reference.

Note

Before using an array, use SetSize to establish its size and allocate memory for it. If you do not use SetSize, adding elements to your array causes it to be frequently reallocated and copied. Frequent reallocation and copying are inefficient and can fragment memory.

Array class derivation is similar to list derivation. For details on the derivation of a special-purpose list class, see the article Collections.

Note

You must use the IMPLEMENT_SERIAL macro in the implementation of your derived class if you intend to serialize the array.

Inheritance Hierarchy

CObject

CObArray

Requirements

Header:afxcoll.h

CObArray::Add

Adds a new element to the end of an array, growing the array by 1.

INT_PTR Add(CObject* newElement);

Parameters

newElement
The CObject pointer to be added to this array.

Return Value

The index of the added element.

Remarks

If SetSize has been used with an nGrowBy value greater than 1, then extra memory may be allocated. However, the upper bound will increase by only 1.

The following table shows other member functions that are similar to CObArray::Add.

ClassMember Function
CByteArrayINT_PTR Add(BYTE newElement);

throw(CMemoryException*);
CDWordArrayINT_PTR Add(DWORD newElement);

throw(CMemoryException*);
CPtrArrayINT_PTR Add(void* newElement);

throw(CMemoryException*);
CStringArrayINT_PTR Add(LPCTSTR newElement); throw(CMemoryException*);

INT_PTR Add(const CString& newElement);
CUIntArrayINT_PTR Add(UINT newElement);

throw(CMemoryException*);
CWordArrayINT_PTR Add(WORD newElement);

throw(CMemoryException*);

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#75]

The results from this program are as follows:

Add example: A CObArray with 2 elements [0] = a CAge at $442A 21 [1] = a CAge at $4468 40 

CObArray::Append

Call this member function to add the contents of another array to the end of the given array.

INT_PTR Append(const CObArray& src);

Parameters

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

Return Value

The index of the first appended element.

Remarks

The arrays must be of the same type.

If necessary, Append may allocate extra memory to accommodate the elements appended to the array.

The following table shows other member functions that are similar to CObArray::Append.

ClassMember Function
CByteArrayINT_PTR Append(const CByteArray& src);
CDWordArrayINT_PTR Append(const CDWordArray& src);
CPtrArrayINT_PTR Append(const CPtrArray& src);
CStringArrayINT_PTR Append(const CStringArray& src);
CUIntArrayINT_PTR Append(const CUIntArray& src);
CWordArrayINT_PTR Append(const CWordArray& src);

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#76]

CObArray::Copy

Call this member function to overwrite the elements of the given array with the elements of another array of the same type.

voidCopy(const CObArray& src);

Parameters

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

Remarks

Copy doesn't free memory. If necessary, Copy may allocate extra memory to accommodate the elements copied to the array.

The following table shows other member functions that are similar to CObArray::Copy.

ClassMember Function
CByteArrayvoid Copy(const CByteArray& src);
CDWordArrayvoid Copy(const CDWordArray& src);
CPtrArrayvoid Copy(const CPtrArray& src);
CStringArrayvoid Copy(const CStringArray& src);
CUIntArrayvoid Copy(const CUIntArray& src);
CWordArrayvoid Copy(const CWordArray& src);

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#77]

CObArray::CObArray

Constructs an empty CObject pointer array.

CObArray();

Remarks

The array grows one element at a time.

The following table shows other constructors that are similar to CObArray::CObArray.

ClassConstructor
CByteArrayCByteArray();
CDWordArrayCDWordArray();
CPtrArrayCPtrArray();
CStringArrayCStringArray();
CUIntArrayCUIntArray();
CWordArrayCWordArray();

Example

[!code-cppNVC_MFCCollections#78]

CObArray::ElementAt

Returns a temporary reference to the element pointer within the array.

CObject*& ElementAt(INT_PTR nIndex);

Parameters

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

Return Value

A reference to a CObject pointer.

Remarks

It's used to implement the left-side assignment operator for arrays. This is an advanced function that should be used only to implement special array operators.

The following table shows other member functions that are similar to CObArray::ElementAt.

ClassMember Function
CByteArrayBYTE& ElementAt(INT_PTR nIndex);
CDWordArrayDWORD& ElementAt(INT_PTR nIndex);
CPtrArrayvoid*& ElementAt(INT_PTR nIndex);
CStringArrayCString& ElementAt(INT_PTR nIndex);
CUIntArrayUINT& ElementAt(INT_PTR nIndex);
CWordArrayWORD& ElementAt(INT_PTR nIndex);

Example

See the example for CObArray::GetSize.

CObArray::FreeExtra

Frees any extra memory that was allocated while the array was grown.

voidFreeExtra();

Remarks

This function has no effect on the size or upper bound of the array.

The following table shows other member functions that are similar to CObArray::FreeExtra.

ClassMember Function
CByteArrayvoid FreeExtra();
CDWordArrayvoid FreeExtra();
CPtrArrayvoid FreeExtra();
CStringArrayvoid FreeExtra();
CUIntArrayvoid FreeExtra();
CWordArrayvoid FreeExtra();

Example

See the example for CObArray::GetData.

CObArray::GetAt

Returns the array element at the specified index.

CObject* GetAt(INT_PTR nIndex) const;

Parameters

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

Return Value

The CObject pointer element currently at this index.

Remarks

Note

Passing a negative value or a value greater than the value returned by GetUpperBound will result in a failed assertion.

The following table shows other member functions that are similar to CObArray::GetAt.

ClassMember Function
CByteArrayBYTE GetAt(INT_PTR nIndex) const;
CDWordArrayDWORD GetAt(INT_PTR nIndex) const;
CPtrArrayvoid* GetAt(INT_PTR nIndex) const;
CStringArrayconst CString& GetAt(INT_PTR nIndex) const;
CUIntArrayUINT GetAt(INT_PTR nIndex) const;
CWordArrayWORD GetAt(INT_PTR nIndex) const;

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#79]

CObArray::GetCount

Returns the number of array elements.

INT_PTR GetCount() const;

Return Value

The number of items in the array.

Remarks

Call this method to retrieve the number of elements in the array. Because indexes are zero-based, the size is 1 greater than the largest index.

The following table shows other member functions that are similar to CObArray::GetCount.

ClassMember Function
CByteArrayINT_PTR GetCount() const;
CDWordArrayINT_PTR GetCount() const;
CPtrArrayINT_PTR GetCount() const;
CStringArrayINT_PTR GetCount() const;
CUIntArrayINT_PTR GetCount() const;
CWordArrayINT_PTR GetCount() const;

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#80]

CObArray::GetData

Use this member function to gain direct access to the elements in the array.

const CObject** GetData() const; CObject** GetData();

Return Value

A pointer to the array of CObject pointers.

Remarks

If no elements are available, GetData returns a NULL value.

While direct access to the elements of an array can help you work more quickly, use caution when calling GetData; any errors you make directly affect the elements of your array.

The following table shows other member functions that are similar to CObArray::GetData.

ClassMember Function
CByteArrayconst BYTE* GetData() const; BYTE* GetData();
CDWordArrayconst DWORD* GetData() const; DWORD* GetData();
CPtrArrayconst void** GetData() const; void** GetData();
CStringArrayconst CString* GetData() const; CString* GetData();
CUIntArrayconst UINT* GetData() const; UINT* GetData();
CWordArrayconst WORD* GetData() const; WORD* GetData();

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#81]

CObArray::GetSize

Returns the size of the array.

INT_PTR GetSize() const;

Remarks

Since indexes are zero-based, the size is 1 greater than the largest index.

The following table shows other member functions that are similar to CObArray::GetSize.

ClassMember Function
CByteArrayINT_PTR GetSize() const;
CDWordArrayINT_PTR GetSize() const;
CPtrArrayINT_PTR GetSize() const;
CStringArrayINT_PTR GetSize() const;
CUIntArrayINT_PTR GetSize() const;
CWordArrayINT_PTR GetSize() const;

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#82]

CObArray::GetUpperBound

Returns the current upper bound of this array.

INT_PTR GetUpperBound() const;

Return Value

The index of the upper bound (zero-based).

Remarks

Because array indexes are zero-based, this function returns a value 1 less than GetSize.

The condition GetUpperBound() = -1 indicates that the array contains no elements.

The following table shows other member functions that are similar to CObArray::GetUpperBound.

ClassMember Function
CByteArrayINT_PTR GetUpperBound() const;
CDWordArrayINT_PTR GetUpperBound() const;
CPtrArrayINT_PTR GetUpperBound() const;
CStringArrayINT_PTR GetUpperBound() const;
CUIntArrayINT_PTR GetUpperBound() const;
CWordArrayINT_PTR GetUpperBound() const;

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#83]

CObArray::InsertAt

Inserts an element (or all the elements in another array) at a specified index.

voidInsertAt( INT_PTR nIndex, CObject* newElement, INT_PTR nCount = 1); voidInsertAt( INT_PTR nStartIndex, CObArray* pNewArray);

Parameters

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

newElement
The CObject 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 GetUpperBound.

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

Remarks

The first version of InsertAt inserts one element (or multiple copies of an element) at a specified index in an array. In the process, it shifts up (by incrementing the index) the existing element at this index, and it shifts up all the elements above it.

The second version inserts all the elements from another CObArray collection, starting at the nStartIndex position.

The SetAt function, in contrast, replaces one specified array element and doesn't shift any elements.

The following table shows other member functions that are similar to CObArray::InsertAt.

ClassMember Function
CByteArrayvoid InsertAt(INT_PTR nIndex, BYTE newElement, int nCount = 1);

throw(CMemoryException*);

void InsertAt(INT_PTR nStartIndex, CByteArray* pNewArray);

throw(CMemoryException*);
CDWordArrayvoid InsertAt(INT_PTR nIndex, DWORD newElement, int nCount = 1);

throw(CMemoryException*);

void InsertAt(INT_PTR nStartIndex, CDWordArray* pNewArray);

throw(CMemoryException*);
CPtrArrayvoid InsertAt(INT_PTR nIndex, void* newElement, int nCount = 1);

throw(CMemoryException*);

void InsertAt(INT_PTR nStartIndex, CPtrArray* pNewArray);

throw(CMemoryException*);
CStringArrayvoid InsertAt(INT_PTR nIndex, LPCTSTR newElement, int nCount = 1);

throw(CMemoryException*);

void InsertAt(INT_PTR nStartIndex, CStringArray* pNewArray);

throw(CMemoryException*);
CUIntArrayvoid InsertAt(INT_PTR nIndex, UINT newElement, int nCount = 1);

throw(CMemoryException*);

void InsertAt(INT_PTR nStartIndex, CUIntArray* pNewArray);

throw(CMemoryException*);
CWordArrayvoid InsertAt(INT_PTR nIndex, WORD newElement, int nCount = 1);

throw(CMemoryException*);

void InsertAt(INT_PTR nStartIndex, CWordArray* pNewArray);

throw(CMemoryException*);

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#84]

The results from this program are as follows:

InsertAt example: A CObArray with 3 elements [0] = a CAge at $45C8 21 [1] = a CAge at $4646 30 [2] = a CAge at $4606 40 

CObArray::IsEmpty

Determines if the array is empty.

BOOL IsEmpty() const;

Return Value

Nonzero if the array is empty; otherwise 0.

CObArray::operator [ ]

These subscript operators are a convenient substitute for the SetAt and GetAt functions.

CObject*& operator[](int_ptr nindex); CObject* operator[](int_ptr nindex) const;

Remarks

The first operator, called for arrays that aren't const, may be used on either the right (r-value) or the left (l-value) of an assignment statement. The second, called for const arrays, may 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.

The following table shows other operators that are similar to CObArray::operator [].

ClassOperator
CByteArrayBYTE& operator [](INT_PTR nindex);

BYTE operator [](INT_PTR nindex) const;
CDWordArrayDWORD& operator [](INT_PTR nindex);

DWORD operator [](INT_PTR nindex) const;
CPtrArrayvoid*& operator [](INT_PTR nindex);

void* operator [](INT_PTR nindex) const;
CStringArrayCString& operator [](INT_PTR nindex);

CString operator [](INT_PTR nindex) const;
CUIntArrayUINT& operator [](INT_PTR nindex);

UINT operator [](INT_PTR nindex) const;
CWordArrayWORD& operator [](INT_PTR nindex);

WORD operator [](INT_PTR nindex) const;

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#88]

CObArray::RemoveAll

Removes all the pointers from this array but doesn't actually delete the CObject objects.

voidRemoveAll();

Remarks

If the array is already empty, the function still works.

The RemoveAll function frees all memory used for pointer storage.

The following table shows other member functions that are similar to CObArray::RemoveAll.

ClassMember Function
CByteArrayvoid RemoveAll();
CDWordArrayvoid RemoveAll();
CPtrArrayvoid RemoveAll();
CStringArrayvoid RemoveAll();
CUIntArrayvoid RemoveAll();
CWordArrayvoid RemoveAll();

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#85]

CObArray::RemoveAt

Removes one or more elements starting at a specified index in an array.

voidRemoveAt( INT_PTR nIndex, INT_PTR nCount = 1);

Parameters

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

nCount
The number of elements to remove.

Remarks

In the process, it shifts down all the elements above the removed element(s). It decrements the upper bound of the array but doesn't free memory.

If you try to remove more elements than are contained in the array above the removal point, then the Debug version of the library asserts.

The RemoveAt function removes the CObject pointer from the array, but it doesn't delete the object itself.

The following table shows other member functions that are similar to CObArray::RemoveAt.

ClassMember Function
CByteArrayvoid RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1);
CDWordArrayvoid RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1);
CPtrArrayvoid RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1);
CStringArrayvoid RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1);
CUIntArrayvoid RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1);
CWordArrayvoid RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1);

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#112]

The results from this program are as follows:

RemoveAt example: A CObArray with 1 elements [0] = a CAge at $4606 40 

CObArray::SetAt

Sets the array element at the specified index.

voidSetAt( INT_PTR nIndex, CObject* newElement);

Parameters

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

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

Remarks

SetAt won't cause the array to grow. Use SetAtGrow if you want the array to grow automatically.

Ensure that your index value represents a valid position in the array. If it's out of bounds, then the Debug version of the library asserts.

The following table shows other member functions that are similar to CObArray::SetAt.

ClassMember Function
CByteArrayvoid SetAt(INT_PTR nIndex, BYTE newElement);
CDWordArrayvoid SetAt(INT_PTR nIndex, DWORD newElement);
CPtrArrayvoid SetAt(INT_PTR nIndex, void* newElement);
CStringArrayvoid SetAt(INT_PTR nIndex, LPCTSTR newElement);
CUIntArrayvoid SetAt(INT_PTR nIndex, UINT newElement);
CWordArrayvoid SetAt(INT_PTR nIndex, WORD newElement);

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#86]

The results from this program are as follows:

SetAt example: A CObArray with 2 elements [0] = a CAge at $47E0 30 [1] = a CAge at $47A0 40 

CObArray::SetAtGrow

Sets the array element at the specified index.

voidSetAtGrow( INT_PTR nIndex, CObject* newElement);

Parameters

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

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

Remarks

The array grows automatically if necessary (that is, the upper bound is adjusted to accommodate the new element).

The following table shows other member functions that are similar to CObArray::SetAtGrow.

ClassMember Function
CByteArrayvoid SetAtGrow(INT_PTR nIndex, BYTE newElement);

throw(CMemoryException*);
CDWordArrayvoid SetAtGrow(INT_PTR nIndex, DWORD newElement);

throw(CMemoryException*);
CPtrArrayvoid SetAtGrow(INT_PTR nIndex, void* newElement);

throw( CMemoryException*);
CStringArrayvoid SetAtGrow(INT_PTR nIndex, LPCTSTR newElement);

throw(CMemoryException*);
CUIntArrayvoid SetAtGrow(INT_PTR nIndex, UINT newElement);

throw(CMemoryException*);
CWordArrayvoid SetAtGrow(INT_PTR nIndex, WORD newElement);

throw(CMemoryException*);

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

[!code-cppNVC_MFCCollections#87]

The results from this program are as follows:

SetAtGrow example: A CObArray with 4 elements [0] = a CAge at $47C0 21 [1] = a CAge at $4800 40 [2] = NULL [3] = a CAge at $4840 65 

CObArray::SetSize

Establishes the size of an empty or existing array; allocates memory if necessary.

voidSetSize( INT_PTR nNewSize, INT_PTR nGrowBy = -1);

Parameters

nNewSize
The new array size (number of elements). Must be greater than or equal to 0.

nGrowBy
The minimum number of element slots to allocate if a size increase is necessary.

Remarks

If the new size is smaller than the old size, then the array is truncated and all unused memory is released. For efficiency, call SetSize to set the size of the array before using it. This prevents the need to reallocate and copy the array each time an item is added.

The nGrowBy parameter affects internal memory allocation while the array is growing. Its use never affects the array size as reported by GetSize and GetUpperBound.

If the size of the array has grown, all newly allocated CObject * pointers are set to NULL.

The following table shows other member functions that are similar to CObArray::SetSize.

ClassMember Function
CByteArrayvoid SetSize(INT_PTR nNewSize, int nGrowBy = -1);

throw(CMemoryException*);
CDWordArrayvoid SetSize(INT_PTR nNewSize, int nGrowBy = -1);

throw(CMemoryException*);
CPtrArrayvoid SetSize(INT_PTR nNewSize, int nGrowBy = -1);

throw(CMemoryException*);
CStringArrayvoid SetSize(INT_PTR nNewSize, int nGrowBy = -1);

throw(CMemoryException*);
CUIntArrayvoid SetSize(INT_PTR nNewSize, int nGrowBy = -1);

throw(CMemoryException*);
CWordArrayvoid SetSize(INT_PTR nNewSize, int nGrowBy = -1);

throw(CMemoryException*);

Example

See the example for CObArray::GetData.

See also

CObject Class
Hierarchy Chart
CStringArray Class
CPtrArray Class
CByteArray Class
CWordArray Class
CDWordArray Class

close