Returning arrays

Top  Previous  Next

What is translated > Returning arrays

 

In Delphi arrays can be returned from functions. This is not possible in C++. Instead a pointer to the first element of the array is returned and used as argument for "memcpy".

 

TObjectArray = array[1..5] of TObject;

function CreateArray: TObjectArray;

 

procedure test;

var

  arr2: TObjectArray;

begin

  arr2 := CreateArray;

end;

 

->

 

typedef TObject* TObjectArray [ 5/*# range 1..5*/ ];

TObject* const * __fastcall CreateArray( );

 

void __fastcall test( )

{

  TObjectArray arr2;

  memcpy( arr2, CreateArray(), sizeof( TObjectArray ) );

}

 

In cases, where the static array is locally constructed inside of a function, a helping array in the file scope is used for an intermediate copy of the array.

 

function CreateArray: TObjectArray;

begin

  result[1] := TObject.Create;

  result[2] := TObject.Create;

  result[3] := TObject.Create;

  result[4] := TObject.Create;

  result[5] := TObject.Create;

end;

 

->

 

TObjectArray ArrayUnit__0;

 

TObject* const * __fastcall CreateArray( )

{

  ArrayUnit__0[1 - 1] = new TObject;

  ArrayUnit__0[2 - 1] = new TObject;

  ArrayUnit__0[3 - 1] = new TObject;

  ArrayUnit__0[4 - 1] = new TObject;

  ArrayUnit__0[5 - 1] = new TObject;

  return ArrayUnit__0;

}

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content