tfuncref45.pp 902 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. program tfuncref45;
  2. {$mode delphi}
  3. {$modeswitch functionreferences}
  4. { test assigning global procedures, methods, and object methods to function
  5. references
  6. same as tfuncref8 but with mode delphi
  7. }
  8. type
  9. TProc = reference to procedure;
  10. procedure CallProc(AProc: TProc);
  11. begin
  12. AProc();
  13. end;
  14. type
  15. TTest = class
  16. class procedure ClassMethod;
  17. procedure InstanceMethod;
  18. end;
  19. var
  20. Acc: Integer;
  21. procedure GlobalProc;
  22. begin
  23. Inc(Acc);
  24. end;
  25. class procedure TTest.ClassMethod;
  26. begin
  27. Inc(Acc, 10);
  28. end;
  29. procedure TTest.InstanceMethod;
  30. begin
  31. Inc(Acc, 100);
  32. end;
  33. var
  34. Proc: TProc;
  35. Obj: TTest;
  36. begin
  37. Proc := GlobalProc;
  38. Proc();
  39. CallProc(GlobalProc);
  40. Proc := TTest.ClassMethod;
  41. Proc();
  42. CallProc(TTest.ClassMethod);
  43. Obj := TTest.Create;
  44. Proc := Obj.InstanceMethod;
  45. Proc();
  46. CallProc(Obj.InstanceMethod);
  47. Obj.Free;
  48. if Acc <> 222 then
  49. halt(Acc);
  50. end.