This document covers important issues in using TArray
The use of a TArray object can avoid the need for the member function of an end-user class to have overloaded variants to handle all possible types of arrays of <class T> elements.
The following example illustrates the point. It constructs a fixed flat array, a variable flat array and a packed array of <TElement> objects.
By taking a TArray<TElement> argument, the CTot::Total() member function can access array elements regardless of the type of array containing them.
class TElement // array elements
{
public:
TElement();
inline TInt GetValue() const;
void SetValue(TInt aVal);
private :
TInt iValue;
};
class CTot : public CBase
{
public :
void Total(const TArray<TElement>& anArray);
private :
TInt iTotalElements;
TInt iTotalValue;
};
TElement::TElement()
: iValue(0)
{}
inline TInt TElement::GetValue() const
{
return iValue;
}
void TElement::SetValue(TInt aVal)
{
iValue = aVal;
}
void CTot::Total(const TArray<TElement>& anArray)
{
TInt count = anArray.Count();
for (TInt jj = 0; jj < count; jj++)
iTotalValue += anArray[jj].GetValue();
iTotalElements += count;
}
Then some code using these could be:
// main body
CArrayFixFlat<TElement>* fix;
CArrayVarFlat<TElement>* var;
CArrayPakFlat<TElement>* pak;
TElement x;
CTot* ptr;
ptr = new CTot;
fix = new CArrayFixFlat<TElement>(2);
var = new CArrayVarFlat<TElement>(2);
pak = new CArrayPakFlat<TElement>(2);
x.SetValue(1);
fix->AppendL(x);
x.SetValue(2);
var->AppendL(x,sizeof(x));
x.SetValue(3);
pak->AppendL(x,sizeof(x));
ptr->Total(fix->Array());
ptr->Total(var->Array());
ptr->Total(pak->Array());
Without the use of TArray , the Total() member function would need overloaded variants to handle all the possible array types which it might expect. The CTot class would need redefining as:
class CTot : public CBase
{
public :
void Total(const CArrayFixFlat<TElement>& anArray);
void Total(const CArrayFixSeg<TElement>& anArray);
void Total(const CArrayVarFlat<TElement>& anArray);
void Total(const CArrayVarSeg<TElement>& anArray);
void Total(const CArrayPakFlat<TElement>& anArray);
private :
TInt iTotalElements;
TInt iTotalValue;
};
To allow the Total() function to handle arrays composed of different types of element, (i.e. arrays constructed with different template values), then a separate variant of Total() is required for each element type. For example, to handle arrays of <TElement1> elements and arrays of <TElement2> elements, the CTot class might be defined as:
class CTot : public CBase
{
public :
void TotalA(const TArray<TElement1>& anArray);
void TotalB(const TArray<TElement2>& anArray);
private :
TInt iTotalElements;
TInt iTotalValue;
};
One important point to note; the referenced TElement returned by operator[] is declared as const . This means that any member function of TElement must also be declared as const , as shown in this example.
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.