tb0156.pp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. { %OPT=-St -Cr }
  2. { Old file: tbs0187.pp }
  3. { constructor in a WIth statement isn't called correct. (works at lest in the case stated) OK 0.99.11 (PM) }
  4. {$static on}
  5. type
  6. Tbaseclass = object
  7. base_arg : longint;
  8. st_count : longint;static;
  9. constructor Init;
  10. destructor Done;
  11. procedure Run; virtual;
  12. end;
  13. Totherclass = object(Tbaseclass)
  14. other_arg : longint;
  15. procedure Run; virtual;
  16. end;
  17. const
  18. BaseRunCount : integer = 0;
  19. OtherRunCount : integer = 0;
  20. constructor Tbaseclass.Init;
  21. begin
  22. writeln('Init');
  23. Inc(st_count);
  24. Run;
  25. end;
  26. destructor Tbaseclass.Done;
  27. begin
  28. writeln('Done');
  29. dec(st_count);
  30. end;
  31. procedure Tbaseclass.Run;
  32. begin
  33. writeln('Base method');
  34. inc(BaseRunCount);
  35. end;
  36. procedure Totherclass.Run;
  37. begin
  38. writeln('Inherited method');
  39. inc(OtherRunCount);
  40. end;
  41. { try this as local vars }
  42. procedure test_local_class_init;
  43. var base1 : TbaseClass;
  44. var other1 : TOtherClass;
  45. begin
  46. with other1 do
  47. Init;
  48. with base1 do
  49. Init;
  50. with other1 do
  51. begin
  52. Writeln('number of objects = ',st_count);
  53. base_arg:=2;
  54. other_arg:=6;
  55. Run;
  56. end;
  57. { test if changed !! }
  58. if (other1.base_arg<>2) or (other1.other_arg<>6) then
  59. Halt(1);
  60. with base1 do
  61. begin
  62. Run;
  63. Done;
  64. end;
  65. other1.done;
  66. end;
  67. var base : Tbaseclass;
  68. other : Totherclass;
  69. testfield : longint;
  70. begin
  71. // Uncommenting here and commenting the init in the WIth solves it.
  72. // Base.Init;
  73. with base do
  74. begin
  75. Init;
  76. Run;
  77. Done;
  78. end;
  79. // Uncommenting here and commenting the init in the WIth solves it.
  80. // Other.init;
  81. with other do
  82. begin
  83. Init;
  84. Run;
  85. Done;
  86. end;
  87. test_local_class_init;
  88. { Calls Tbaseclass.Run when it should call Totherclass.Run }
  89. If (BaseRunCount<>4) or (OtherRunCount<>4) then
  90. Begin
  91. Writeln('Error in tb162');
  92. Halt(1);
  93. End;
  94. end.