texttransformer.jpg

The corresponding C++ feature to Delphi's anonymous methods are lambda expressions. The translation is quite straight forward:


An anonymous method type can be declared as a reference to a method. It becomes in C++ to a std::function type:


type
  TFuncOfInt = reference to function(x: Integer): Integer;

var
  adder: TFuncOfInt;
begin
  adder :=  function(X: Integer) : Integer
  begin
    Result := X + Y;
    end;
  WriteLn(adder(22)); // -> 42
->

typedef std::function TFuncOfInt;

 TFuncOfInt adder;
 adder = [&](int X) -> int {
  int result = 0;
  result = X + Y;
  return result;
  };
 WriteLn(adder(22)); // -> 42



As well as anonymous methods can be assigned to a method reference (see above), a normal method can be assigned to it. In C++ this is done by means of std::bind. The expression of this assignment looks quite complicated however, because std::placeholders are needed to represent unbound variables.



type
  TMethRef = Reference to procedure(X: Integer);

TAn3Class = class(TObject)
  procedure method(X: Integer);
end;

procedure Test;  
var
  m: TMethRef;
  i: TAn3Class;
begin
  // ...
  m := i.method;   
end;

->

typedef std::function TMethRef;

class TAn3Class : public System::TObject
{
  typedef System::TObject inherited;
public:
  void method(int X);
public:
  TAn3Class() {}
};

void Test()
{
  TMethRef m;
  TAn3Class* i = nullptr;
  // ...
  m = std::bind(&TAn3Class::method, i, std::placeholders::_1); 
}













   deutsch Deutsch

 

 
Latest News
08/26/24
Delphi2Cpp 2.4: Updated to RAD Studio 12.1 Athens [more...]

01/29/24
Aurora2Cpp: Delphi 7 translator [more...]



"Thanks for your great work, really appreciate the work you have done on zlib and compiling ... test case."


Mattewada, Udayabhaskar
Nokia India 02/01/2021




[from case study...]

"A masterpiece -- Delphi2Cpp has exceeded all my expectations by far."


Tony Hürlimann
virtual-optima 08/20/2011



"First off, I have to say WOW! Delphi2Cpp is doing a *fantastic* job!"


Daniel Flower
linkrealms 01/15/2011


 
This website is generated from plain text with [Minimal Website ]

Minimal Website
Minimal Website is made with TextTransformer

TextTransformer
TextTransformer is made with Borland CBuilder

  borland