tobjsiz2.pp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. { Variation without virtual classes : no VMT }
  2. { here sizeof directly returns a constant value }
  3. {$static on}
  4. type
  5. pbaseclass = ^tbaseclass;
  6. pderivedclass = ^tderivedclass;
  7. tbaseclass = object
  8. x : byte;
  9. function getsize : longint; static;
  10. procedure check_size;
  11. end;
  12. tderivedclass = object(tbaseclass)
  13. y : byte;
  14. end;
  15. const
  16. expected_size_for_tbaseclass = sizeof(byte);
  17. expected_size_for_tderivedclass = 2*sizeof(byte);
  18. var
  19. basesize : longint;
  20. derivedsize : longint;
  21. function tbaseclass.getsize : longint;
  22. begin
  23. { self = pointer to VMT }
  24. getsize:=sizeof(self);
  25. end;
  26. procedure tbaseclass.check_size;
  27. begin
  28. if getsize<>sizeof(pointer) then
  29. begin
  30. Writeln('Compiler creates garbage ',sizeof(self),'<>',sizeof(pointer));
  31. halt(1);
  32. end;
  33. end;
  34. var
  35. cb : tbaseclass;
  36. cd : tderivedclass;
  37. c1 : pbaseclass;
  38. begin
  39. new(c1);
  40. basesize:=sizeof(cb);
  41. Writeln('Sizeof(cb)=',basesize);
  42. if basesize<>expected_size_for_tbaseclass then
  43. begin
  44. Writeln('not the expected size : ',expected_size_for_tbaseclass);
  45. halt(1);
  46. end;
  47. derivedsize:=sizeof(cd);
  48. Writeln('Sizeof(ct)=',derivedsize);
  49. if derivedsize<>expected_size_for_tderivedclass then
  50. begin
  51. Writeln('not the expected size : ',expected_size_for_tderivedclass);
  52. halt(1);
  53. end;
  54. cb.check_size;
  55. end.