Nested functions

Top  Previous  Next

What is translated > Nested functions

 

There aren't nested functions in C++. The automatic translation of nested Delphi functions replaces the inner functions by new member functions. The parameters  and the declared variables of the outer function are passed to these new functions.

 

 

type

TNested = class

public

iClassVar : Integer;

function Test(iOuterParam, iTwiceParam : Integer; s : String): Integer;

end;

 

implementation

 

function TNested.Test(iOuterParam, iTwiceParam : Integer; s : String): Integer;

const

  cSeparate = ':';

var

 iFunctionVar : Integer;

 

 procedure NestedTest(iInnerParam, iTwiceParam : Integer);

 begin

 result := iClassVar + iOuterParam + iFunctionVar + iInnerParam + iTwiceParam;

 end;

 

begin

 iClassVar := 1;

 iFunctionVar := 2;

 NestedTest1(3, 4);

 result := result + iTwiceParam;

end;

 

->

 

class TNested: public System::TObject {

  public:

  int iClassVar;

  int __fastcall Test( int iOuterParam, int iTwiceParam, String s );

  public: inline __fastcall TNested () {}

  private: // originally nested

  void __fastcall NestedTest( int iInnerParam, int iTwiceParam, int& iFunctionVar, int& iOuterParam, int& xResult );

};

 

void __fastcall TNested::NestedTest( int iInnerParam, int iTwiceParam, int& iFunctionVar, int& iOuterParam, int& xResult )

{

  xResult = iClassVar + iOuterParam + iFunctionVar + iInnerParam + iTwiceParam;

}

 

 

int __fastcall TNested::Test( int iOuterParam, int iTwiceParam, String s )

{

  int result = 0;

  const char cSeparate = ':';

  int iFunctionVar = 0;

  iClassVar = 1;

  iFunctionVar = 2;

  NestedTest1( 3, 4, iFunctionVar, iOuterParam, result );

  result = result + iTwiceParam;

  return result;

}

 

 

int __fastcall TNested::Test ( int iOuterParam, int iTwiceParam )

{

  int result;

  int iFunctionVar;

  iClassVar = 1;

  iFunctionVar = 2;

  NestedTest( 3, 4, iOuterParam, result, iFunctionVar );

  result = result + iTwiceParam;

  return result;

}

 

 

From Delphi2Cpp 1.5.2 on only the parameters, variables and constants are passed, which are actually required. It is taken into account that at multiple nesting possible parameters which aren't needed in a function itself but in a function subordinated to it are passed through. The "result" variable for the return value of the topmost function is submitted to subordinate functions under the name "xResult" and therefore is distinguished from the "result" variables there.

 

Other possibilities to translate nested functions are discussed here:

 

http://www.gotw.ca/gotw/058.htm

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content