Function Pointer ex03



Function Pointer example 03
Smart pascal source code
type TMyFunc = function(s: String): String; type TMyRec = record Proc1: TMyFunc; Proc2: TMyFunc; end; type TMyClass = class FRec: TMyRec; FAFunc: TMyFunc; procedure WriteLn; function GetProc1: TMyFunc; procedure SetProc1(f: TMyFunc); property AFunc: TMyFunc read FAFunc write FAFunc; end; procedure TMyClass.WriteLn; begin WriteLn(FRec.Proc1('World')); WriteLn(FRec.Proc2('world!')); WriteLn(FAFunc('world')); end; function TMyClass.GetProc1: TMyFunc; begin Result := FRec.Proc1; end; procedure TMyClass.SetProc1(f: TMyFunc); begin FRec.Proc1 := f; end; function Func1(s: String): String; begin Result := 'Hello ' + s; end; function Func2(str: String): String; begin Result := 'ByeBye ' + str; end; function Func3(str: String): String; begin Result := 'Ho ho ho ' + str; end; var o := TMyClass.Create; o.FRec.Proc1 := Func1; o.FRec.Proc2 := Func2; o.AFunc := Func3; o.WriteLn; WriteLn(o.GetProc1()('get')); WriteLn(o.AFunc('direct prop')); var old: TMyFunc := o.GetProc1(); o.SetProc1(Func2); WriteLn(old('old')); old := o.FRec.Proc1; WriteLn(old('new')); {<<< RESULT - CONSOLE LOG >>> ----------------------------- Hello World ByeBye world! Ho ho ho world Hello get Ho ho ho direct prop Hello old ByeBye new ----------------------------- {<<<<<<<<< THE END >>>>>>>>>}