Array::SetAtGrow

Description

Adds a reference to the passed object newElement, at the indicated index nIndex. Enlarges the array if needed.

Syntax

BOOL SetAtGrow( int nIndex, _TemplType newElement )

Parameters

nIndex
[input] The array index where the reference of the object needs to be added, if nIndex is larger than the upper-bound, the size is increased to fit.
newElement
[input] The reference of the new item to be set at nIndex

Return

Returns a boolean indicating success or failure of the requested operation

Examples

EX1

#include <Array.h>
struct PERSON
{
        int nID;
};

void Array_SetAtGrow_ex1()
{
        Array<PERSON&> arp(true);
        int nTotal = 5;
        for ( int ii = 0; ii < nTotal; ii++ )
        {
                PERSON* pp = new PERSON;
                //assume memory allocated successfully.
                pp->nID = ii + 1;
                arp.Add(*pp);
        }
        
        PERSON* p = new PERSON;
        p->nID = 999;
        arp.SetAtGrow(10, *p);//after set, the array's size will be 11
        int nSize = arp.GetSize();
        for ( ii = 0; ii < nSize; ii++ )
        {
                PERSON& pm = arp.GetAt(ii);
                if ( pm ) //some item maybe invalid, so need to check before use it.
                {
                        printf("PERSON %d's ID is : %d\n", ii + 1, pm.nID);
                }
        }
}

Remark

See Also

Array::SetAt

header to Include

Array.h