Use as events

Top  Previous  Next

New features since Delphi 7 > Anonymous Methods > Use as events

Delphi “anonymous method” types (e.g. reference to procedure) are not of object. They are closures and can naturally be represented as std::function in C++.

 

Delphi

type

  TAnProc = reference to procedure;

 

  TAn4Component = class(TComponent)

  private

    FMyEvent: TAnProc;

  public

    property MyEvent: TAnProc read FMyEvent write FMyEvent;

  end;

 

procedure TestAnonymous4;

var

  C: TAn4Component;

begin

  C := TAn4Component.Create;

  C.MyEvent := procedure begin end;  // assign lambda

end;

 

C++

using TAnProc = std::function<void()>;

 

class TAn4Component : public System::TComponent {

  TAnProc FMyEvent;

public:

  TAnProc  ReadPropertyMyEvent() const { return FMyEvent; }

  void     WritePropertyMyEvent(TAnProc v) { FMyEvent = std::move(v); }

};

 

void TestAnonymous4()

{

  auto* C = new TAn4Component();

  C->WritePropertyMyEvent([](){ /* ... */ });

  // if (auto cb = C->ReadPropertyMyEvent()) cb();

  delete C;

}

 

 

 

 

 

 

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content