bug0312.pp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. uses
  2. objects;
  3. type
  4. {$ifndef FPC}
  5. sw_integer = integer;
  6. {$endif not FPC}
  7. PMYObj = ^TMyObj;
  8. TMyObj = Object(TObject)
  9. x : longint;
  10. Constructor Init(ax : longint);
  11. procedure display;virtual;
  12. end;
  13. PMYObj2 = ^TMyObj2;
  14. TMyObj2 = Object(TMyObj)
  15. y : longint;
  16. Constructor Init(ax,ay : longint);
  17. procedure display;virtual;
  18. end;
  19. PMyCollection = ^TMyCollection;
  20. TMyCollection = Object(TCollection)
  21. function At(I : sw_integer) : PMyObj;
  22. procedure DummyThatShouldNotBeCalled;virtual;
  23. end;
  24. { TMy is also a TCollection so that
  25. ShowMy and DummyThatShouldNotBeCalled are at same position in VMT }
  26. TMy = Object(TCollection)
  27. Col : PMyCollection;
  28. MyObj : PMyObj;
  29. ShowMyCalled : boolean;
  30. constructor Init;
  31. destructor Done;virtual;
  32. procedure ShowAll;
  33. procedure AddMyObj(x : longint);
  34. procedure AddMyObj2(x,y : longint);
  35. procedure ShowMy;virtual;
  36. end;
  37. Constructor TMyObj.Init(ax : longint);
  38. begin
  39. Inherited Init;
  40. x:=ax;
  41. end;
  42. Procedure TMyObj.Display;
  43. begin
  44. Writeln('x = ',x);
  45. end;
  46. Constructor TMyObj2.Init(ax,ay : longint);
  47. begin
  48. Inherited Init(ax);
  49. y:=ay;
  50. end;
  51. Procedure TMyObj2.Display;
  52. begin
  53. Writeln('x = ',x,' y = ',y);
  54. end;
  55. Function TMyCollection.At(I : sw_integer) : PMyObj;
  56. begin
  57. At:=Inherited At(I);
  58. end;
  59. Procedure TMyCollection.DummyThatShouldNotBeCalled;
  60. begin
  61. Writeln('This method should never be called');
  62. Abstract;
  63. end;
  64. Constructor TMy.Init;
  65. begin
  66. New(Col,Init(5,5));
  67. MyObj:=nil;
  68. ShowMyCalled:=false;
  69. end;
  70. Destructor TMy.Done;
  71. begin
  72. Dispose(Col,Done);
  73. Inherited Done;
  74. end;
  75. Procedure TMy.ShowAll;
  76. procedure ShowIt(P : pointer);{$ifdef TP}far;{$endif}
  77. begin
  78. ShowMy;
  79. PMyObj(P)^.Display;
  80. end;
  81. begin
  82. Col^.ForEach(@ShowIt);
  83. end;
  84. Procedure TMy.ShowMy;
  85. begin
  86. if assigned(MyObj) then
  87. MyObj^.Display;
  88. ShowMyCalled:=true;
  89. end;
  90. Procedure TMy.AddMyObj(x : longint);
  91. begin
  92. MyObj:=New(PMyObj,Init(x));
  93. Col^.Insert(MyObj);
  94. end;
  95. Procedure TMy.AddMyObj2(x,y : longint);
  96. begin
  97. MyObj:=New(PMyObj2,Init(x,y));
  98. Col^.Insert(MyObj);
  99. end;
  100. var
  101. My : TMy;
  102. begin
  103. My.Init;
  104. My.AddMyObj(5);
  105. My.AddMyObj2(4,3);
  106. My.AddMyObj(43);
  107. { MyObj field is now a PMyObj with value 43 }
  108. My.ShowAll;
  109. If not My.ShowMyCalled then
  110. begin
  111. Writeln('ShowAll does not work correctly');
  112. Halt(1);
  113. end;
  114. My.Done;
  115. end.