The following is a simple sample of using a CArray. This sample is kept especially simple by making it an array of doubles. The first thing to do is to make a typedef, as in:
typedef CArray <double, double> CArrayType;
Next, use the typedef as in the following:
CArrayType Doubles;
Then add entries as in (where "d" is a "double"):
Doubles.Add(d);
Then iterate through the list as in the following:
int n = Doubles.GetSize();
for (Index=0; Index<n; ++Index) {
d = Doubles[Index]
}
Finally, remove all the elements as in:
Doubles.RemoveAll;
Notice that since the list contents are not pointers, there is not any clean-up necessary.
To make a CArray of a CArray of a Fundamental Type you can make a CArray as in the following:
class CDoublesArray : public CArray<double, double> {
public:
void operator= (const CDoublesArray& DoublesArray) {
RemoveAll();
Append(DoublesArray);
}
};
Then use it to make a CArray as in:
CArray<CDoublesArray, CDoublesArray&> ArrayArray; CDoublesArray DoublesArray; DoublesArray.Add(9); DoublesArray.Add(99); ArrayArray.Add(DoublesArray);
If you need to make a CArray of a CArray of a class or structure, then the first-level CArray needs a copy constructor. So if we had a class such as:
class CBase {
public:
int BasePoly;
};
Then we can create a CArray of that class as in:
class CBaseArray : public CArray<CBase, CBase&> {
public:
CBaseArray() : CArray<CBase, CBase&>() {};
CBaseArray(const CBaseArray &BaseArray) {
RemoveAll();
Append(BaseArray);
}
void operator= (const CBaseArray &BaseArray) {
RemoveAll();
Append(BaseArray);
}
};
Then the following is a sample of using the CBaseArray array in another CArray, essentially creating a two-dimensional array;
int i1, i2;
CBase Base;
CBaseArray BaseArray;
CArray<CBaseArray, CBaseArray&> BaseArrayArray;
Base.BasePoly = 99;
BaseArray.Add(Base);
Base.BasePoly = 98;
BaseArray.Add(Base);
Base.BasePoly = 59;
BaseArray.Add(Base);
BaseArrayArray.Add(BaseArray);
BaseArray.RemoveAll();
Base.BasePoly = 9;
BaseArray.Add(Base);
Base.BasePoly = 8;
BaseArray.Add(Base);
Base.BasePoly = 5;
BaseArray.Add(Base);
BaseArrayArray.Add(BaseArray);
for (i1=0; i1<BaseArrayArray.GetSize(); ++i1) {
for (i2=0; i2<BaseArrayArray[i1].GetSize(); ++i2) {
// Do something with BaseArrayArray[i1][i2]
}
}
See my Visual C++ Programmer Stuff page for more C++ stuff.