RAD Studio VCL Reference
|
System::DynamicArray is the C++ analog for the Delphi dynamic array type.
DynamicArray<class T> = class;
template <class T> class DynamicArray;
A Dynamic Array is a resizable collection implemented as an array. The System::DynamicArray template is a C++ implementation of Delphi Dynamic Array types. The System::DynamicArray template uses a binary layout compatible with the Delphi Dynamic Array implementation.
template <class T> class DELPHIRETURN DynamicArray;
You must specify one template parameter:
Parameter |
Usage |
type |
the type of the elements |
Length of dynamic arrays
The size of a System::DynamicArray can be set or obtained from the Length property:
//C++ example
To free a System::DynamicArray, assign it a Length of zero.
Accessing data
Access elements of a Dynamic Array via the subscript [] operator:
//C++ example
void InitArray(DynamicArray<char> &c_array) { c_array[0] = 'A'; c_array[1] = 'B'; cout << "Third char is: " << c_array[2]; }
Bounds of dynamic arrays
The Low and High properties of System::DynamicArrays return the low and high bounds of the array respectively.
//C++ example
Assigning, comparing and copying dynamic arrays
Dynamic Arrays are reference counted. When a System::DynamicArray is assigned to another, only the reference is assigned (and the reference count adjusted), the content of the source is not copied. Similarly, when two Dynamic Arrays are compared, only the references are compared, not the contents. To copy the contents of a System::DynamicArray, you must use the Copy (or CopyRange) methods.
//C++ example
void foo(DynamicArray<int> &i_array) { DynamicArray<int> temp = i_array; assert(temp == i_array); // temp and i_array point to same chunk // of data.. i_array[0] = 20; assert(temp[0] == 20); // Temp 'sees' changes to i_array.. temp = i_array.Copy(); // Temp gets its own copy of data temp[0] = 10; assert(temp[0] != i_array[0]); // Above assignment did not // modify i_array. }
Multidimensional dynamic arrays
Dynamic arrays can be multidimensional. Multidimensional dynamic arrays don't have to be rectangular. The Length property can be used to set each dimension. The following example illustrates.
//C++ example
typedef DynamicArray< DynamicArray < AnsiString > > T2DStringArray; void foo(T2DStringArray &s_array) { SetLength(s_array, 10); for (int i=0; i<s_array.Length; i++) { // Set lengths of second dimensions.(NOTE: non-rectangular) SetLength(s_array[i], i+1); for (int j=0; j<s_array[i].Length; j++) /* */ s_array[i][j] = itoa(i*10+j); } }
C++ & Delphi System::DynamicArrays
A dynamic array declared in C++ code can be used in Delphi code and vice versa. The following examples illustrate passing a one-dimensional and two-dimensional array between C++ and Delphi.
C++ System::DynamicArray Example
Delphi System::DynamicArray Example
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|