Nested routines with C++98 |
Top Previous Next |
What is translated > Routines > Nested routines > Nested routines with C++98 There aren't nested functions in C++. For C++98 in contrast to C++11 the automatic translation of nested Delphi functions replaces the inner functions by new free functions or 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 { #include "Test_friends.inc" public: int iClassVar; void __fastcall NestedTest(int iInnerParam, int iTwiceParam, int& result, int& iOuterParam, int& iFunctionVar); int __fastcall Test(int iOuterParam, int iTwiceParam, System::String s); };
void __fastcall TNested::NestedTest(int iInnerParam, int iTwiceParam, int& result, int& iOuterParam, int& iFunctionVar) { result = iClassVar + iOuterParam + iFunctionVar + iInnerParam + iTwiceParam; };
int __fastcall TNested::Test(int iOuterParam, int iTwiceParam, String s) { int result = 0; const WideChar cSeparate = L':'; int iFunctionVar = 0; iClassVar = 1; iFunctionVar = 2; NestedTest(3, 4, result, iOuterParam, iFunctionVar); result = result + iTwiceParam; return result; }
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.
In contrast to the solution with lambda functions however, there may be difficulties with undeclared types at the declaration of the inner functions.
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 |