tw18058b.pp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. { %fail }
  2. {$ifdef fpc}{$mode objfpc}{$H+}{$endif}
  3. uses
  4. Classes;
  5. type
  6. IIntf1 = interface
  7. ['{87776F0F-8CE0-4881-B969-C76F5A9CA517}']
  8. procedure M1;
  9. end;
  10. IIntf2 = interface
  11. ['{923C47DF-0A7E-4698-98B8-45175306CDF2}']
  12. procedure M2;
  13. end;
  14. { TObjIntf2 }
  15. TObjIntf2 = class(TInterfacedObject, IIntf2)
  16. procedure M2;
  17. end;
  18. { TObj }
  19. TObj = class(TInterfacedObject, IIntf1, IIntf2)
  20. private
  21. FObjIntf2:IIntf2;
  22. public
  23. constructor Create;
  24. procedure M1;
  25. property I2:IIntf2 read FObjIntf2 implements IIntf2;
  26. // method resolution after delegation, forbidden
  27. procedure IIntf2.M2 = _M2;
  28. procedure _M2;
  29. end;
  30. { TObjIntf2 }
  31. procedure TObjIntf2.M2;
  32. begin
  33. Writeln('TObjIntf2.M2 called');
  34. end;
  35. { TObj }
  36. constructor TObj.Create;
  37. begin
  38. FObjIntf2:=TObjIntf2.Create;
  39. end;
  40. procedure TObj.M1;
  41. begin
  42. Writeln('TObj.M1 called');
  43. end;
  44. procedure TObj._M2;
  45. begin
  46. Writeln('TObj.M2 called');
  47. end;
  48. var O:TObj;
  49. i1:IIntf1;
  50. i2:IIntf2;
  51. begin
  52. O:=TObj.Create;
  53. i1:=O;
  54. //all tries are unsuccessful
  55. i2:=O as IIntf2;
  56. //(O as IIntf1).QueryInterface(IIntf2, i2);
  57. // i1.QueryInterface(IIntf2, i2);
  58. //still calls TObj1.M1
  59. i2.M2;
  60. end.