Temporary variables

Top  Previous  Next

What is translated > Temporary variables

 

In Delphi it is possible to pass combinations of string literals with strings as parameters like in the following example:

 

function Greet(Msg : PChar): Boolean;

begin

  // doing something with Msg

end;

 

procedure GreetSomeone(Name : String);

begin

 if Greet(PChar('hello ' + Name + '!')) then

   Exit;

 ...

end;  

 

In C++ a string literal can be added to a string, but not the other way round. In such cases Delphi2Cpp automatically creates a temporary string from the string literal to which the following strings and string literals can be added, like:

 

String( "hello " ) + Name + "!"; 

 

To make a character pointer from this construct, another temporary string would have to be created, like:

 

String(String( "hello " ) + Name + "!").c_str(); 

 

But, if such a construct would be passed to a function like:

 

bool __fastcall Greet( char* Msg )

{

  // doing something with Msg

}

 

the resulting character pointer is destroyed as soon as the destructors of the temporary strings is executed. So, inside of the body of the called function, the character pointer isn't valid any more. Therefore a temporary variable is created and enclosed into a block together with the statement of the function call:

 

void __fastcall GreetSomeone( String Name )

{

  {

    AnsiString Str__0 = AnsiString( "hello " ) + Name + "!";

    if ( Greet( Str__0.c_str( ) ) ) 

      return;;

  }

  ...

}

 

 

In a similar way temporary variables are constructed for temporary array parameters:

 

procedure Log(strings : array of String);

 

Log(['one', 'two', 'three']);

 

This becomes to:

 

void __fastcall Log( const String* strings, int strings_maxidx )

 

{

  String tmp__0[ 3 ];

  tmp__0[ 0 ] = "one";

  tmp__0[ 1 ] = "two";

  tmp__0[ 2 ] = "three";

  Log( tmp__0, 3 );

}

 

A special case is "array of const". This case is handled by a macro.

If a function has a set-Parameter, temporary sets are constructed in the C++ translation by means of a definition.



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content