Assignment to a method reference

Top  Previous  Next

New features since Delphi 7 > Anonymous Methods > Assignment to a method reference

 

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<int (int)> TFuncOfInt;

 

 TFuncOfInt adder;

 adder = [&](int X) -> int {

  int result = 0;

  result = X + Y;

  return result;

  };

 WriteLn(adder(22)); // -> 42

 

 

 

 

Here the example from Embarcadero is simplified to remove a problem, which is discussed in the context of variable binding.

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content