procedure test(const a: array of Integer);
This is an open array passed as const
. They are already transmitted by reference. Adding [ref]
is useless in this situation.
Only if you pass the open array by value will a copy be made:
procedure test(a: array of Integer);
Another option, for completeness, should go through var
.
procedure test(var a: array of Integer);
Here the array is passed by reference, but, unlike the const
array, the compiler allows its contents to be changed.
I know the same code in Delphi ...
This is not entirely accurate. Probably the best mapping from C ++ std::vector<T>
is Delphi TList<T>
. Probably the closest match to the Delphi open array parameter will be the C ++ array parameter. You can map your Delphi procedure:
procedure test(const a: array of Integer);
for this C ++ function:
void test(const int a[], const size_t len);
Thus, you really can not compare, for example, as.
However, Delphi dynamic arrays, which you could use when you actually call such a function, are managed types. This means that their lifetime is controlled by automatic reference counting (ARC) and distinguishes them from raw C ++ arrays.
I'm chatting a bit now. Basically I try to understand that the devil is in the details. None of these things fits perfectly between these languages, because languages have subtle differences.
But leaving these nuances aside, if you want to efficiently pass an array to Delphi, then an open const
array will achieve this.