Assignment to a method

Top  Previous  Next

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

 

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

}

 

 

 

 

 

 

 

 

 

 

 

 



This page belongs to the DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content