Open array parameters |
Top Previous Next |
What is translated > Types > Arrays > Array parameters > Open array parameters
The concept of open arrays allow arrays of different sizes to be passed to the same procedure or function.
function Sum(Arr: Array of Integer): Integer; var i: Integer; begin Result := 0; for i := Low(Arr) to High(Arr) do Result := Result + Arr[i]; end;
In C++ there is no counterpart to the function High, which typically is needed to use the open array. Therefore in C++ the value of the upper bound of the open array has to be passed together with a pointer to the first element of the array.
For C++Builder:
int __fastcall Sum(const int* Arr, int Arr_maxidx ) { int result; int i; result = 0; for ( i = 0 /* Low( Arr )*/; i <= Arr_maxidx /* High( Arr )*/; i++) result = result + Arr[i]; return result; }
For other compilers a vector is passed:
int Sum(const std::vector<int> Arr) { int result = 0; int i = 0; int stop = 0; result = 0; for(stop = (int) Arr.size() - 1 /*# High(Arr) */, i = 0 /*# Low(Arr) */; i <= stop; i++) { result = result + Arr[i]; } return result; }
If a temporary set of values is passed as open array parameter to a function, a corresponding array is produced in the C++ output, which is put in front of the function call.
The function call
Sum([1,2,3]);
is converted for C++ Builder by use of the OPENARRAY macro, which is defined in sysopen.h:
Sum(OPENARRAY(int, (1, 2, 3)));
For other compilers the call is simply converted to
Sum({1, 2, 3});
A special case of open array parameters is the use as var-parameter
|
This page belongs to the Delphi2Cpp Documentation |
Delphi2Cpp home Content |