Descriptors...
Section #1: Arrays
1. Dynamic Arrays
NOTE: We didn't specify the size anywhere
ARRAYS OF POINTERS (to CBase derived objects)
2. Fixed Arrays
Section #2: Buffers And Strings
1. Dynamic Arrays
General fixed length array using a flat buffer
CArrayFixFlat<TElement>* fixflat;
// Construct array of TElement objects where the iData
// data member of each element contains "X0", "X1", etc
// Uses the AppendL() function to add the members
// and the [] operator to access elements.
fixflat = new (ELeave) CArrayFixFlat<TElement>(3);
TElement theElement;
_LIT(KFormat1,"X%u");
for (value = 0; value < 3; value++) {
theElement.iData.Format(KFormat1,value);
fixflat->AppendL(theElement);
}
NOTE: We didn't specify the size anywhere
// Show each element
for (forix = 0; forix < fixflat->Count(); forix++)
console->Printf(KCommonFormat4,&(*fixflat)[forix].iData);
// Insert Elements
fixflat->InsertL(0,theElement);
// Delete Elements
fixflat->Delete(1); // delete the 2nd
fixflat->Delete(2,2); // delete what are now the 3rd and 4th
fixflat->Compress(); // compress the array
// reset the array (i.e. empty it)
fixflat->Reset();
// Expand by constructing a new element at position 1.
// Extend the array.
//
// Note the use of the TElement default constructor
// in both cases
fixflat->ExpandL(1);
fixflat->ExtendL();
// Resize the array so that it only contains one element
fixflat->ResizeL(1);
// Resize so that it contains 3 elements. The two new elements are bit-wise copies
// of a TElement object constructed using its default constructor.
fixflat->ResizeL(3);
// Resize so that it contains 5 elements.
// The two new elements are bit-wise copies of the TElement object passed through
// the reference.
_LIT(KMsgXXX,"XXX");
theElement.iData = KMsgXXX;
fixflat->ResizeL(5,theElement);
// Reserve sufficient space to append another five elements.
// This function may leave if there is insufficient memory.
// NOTE: this does NOT increase the number of elements in the array.
fixflat->SetReserveL(5);
// We can now append five elements and be sure that no leave will occur.
ARRAYS OF POINTERS (to CBase derived objects)
CArrayPtrFlat<CElement>* ptrflat;
CElement* ptr;
// Construct an array of CElement objects each containing the text "First" "second" etc
// Uses the AppendL() function to add the members and the [] operator to access elements.
// Construction & initialization
ptrflat = new (ELeave) CArrayPtrFlat(16);
ptr = new (ELeave) CElement;
_LIT(KMsgFirstElement,"First Element");
ptr->SetTextL(KMsgFirstElement);
ptrflat->AppendL(ptr);
ptr = new (ELeave) CElement;
_LIT(KMsgSecondElement,"Second Element");
ptr->SetTextL(KMsgSecondElement);
ptrflat->AppendL(ptr);
// Insert an element at the beginning of the array.
ptr = new (ELeave) CElement;
_LIT(KMsgInsertedBeg,"Inserted @ beginning Element");
ptr->SetTextL(KMsgInsertedBeg);
ptrflat->InsertL(0,ptr);
// Show number of elements
console->Printf(KCommonFormat5,ptrflat->Count());
// Show each element
for (forix = 0; forix < ptrflat->Count(); forix++)
console->Printf(KCommonFormat4,((*ptrflat)[forix])->iBuf);
// Delete the last two elements from the array BUT
// first we must get a reference to those elements
// (pointers to CElement objects) otherwise
// the CElement objects are orphaned.
//
// Here, we destroy those CElement objects.
//
// There are two alternative ways of indexing into
// the array, using either At() or the [] operator
// NOTE that the code below could be compressed to:
//
// delete (*ptrflat)[ptrflat->Count()-1];
// delete (*ptrflat)[ptrflat->Count()-2];
// ptrflat->Delete(ptrflat->Count()-2,2);
// Make a change to the object pointed to by the first element in the array
_LIT(KMsgNewTextFirst,"New text for the first CElement");
_LIT(KMsgNewTextSecond,"New text for the second CElement");
(*ptrflat)[0]->SetTextL(KMsgNewTextFirst);
ptrflat->At(1)->SetTextL(KMsgNewTextSecond);
// Destroy all of the CElement objects and reset the array.
ptrflat->ResetAndDestroy();
// Delete the array
delete ptrflat;
2. Fixed Arrays
Section #2: Buffers And Strings