Recent

Author Topic: Helpers for TObject  (Read 1773 times)

damieiro

  • Jr. Member
  • **
  • Posts: 89
Re: Helpers for TObject
« Reply #15 on: December 09, 2018, 12:43:21 pm »
Even Better...

If i change the helper declaration (declaring it first), the behaviour changes! In previous code TObject2 doesn't catch the helper...
So it seems that compiler uses the declarative order. :) -> These are the kinds of things i'm testing ;)

Code: Pascal  [Select]
  1.  
  2. program helpers;
  3.  
  4. uses classes, sysutils;
  5.  
  6.  
  7. type
  8.  
  9. TObjectHelper = class helper for TObject
  10.   public
  11.   procedure AfterConstruction; overload;{inherited wouldn't call it}
  12.   procedure BeforeDestruction; {overload; inherited would call it}
  13. end;
  14.  
  15. type
  16.  
  17. TObject2=class
  18. constructor create;
  19. destructor destroy;override;
  20.  
  21. end;
  22.  
  23. constructor Tobject2.create;
  24. begin
  25.   {do things}
  26.   writeln ('tobject2 create init');
  27.   inherited afterconstruction; {do nothing}
  28.   self.afterconstruction;   {do nothing}
  29.   writeln ('tobject2 create end');
  30.  
  31.  
  32. end;
  33.  
  34. destructor Tobject2.destroy;
  35. begin
  36.   writeln ('tobject2 destructor init');
  37.  
  38.   inherited beforedestruction;
  39.   self.beforedestruction; {}
  40.   writeln ('tobject2 destructor end');
  41.   {do things}
  42. end;
  43.  
  44.  
  45.  
  46.  
  47. procedure TObjectHelper.AfterConstruction;
  48. begin
  49.   writeln ('Buid TObject:',self.classname);
  50. end;
  51.  
  52. procedure TObjectHelper.BeforeDestruction;
  53. begin
  54.   writeln ('Destroyed TObject:',self.classname);
  55. end;
  56.  
  57.  
  58. type
  59. TDescendant= class (Tobject2)
  60.   public
  61.   constructor create;
  62.   destructor destroy; override;
  63.  
  64.   procedure AfterConstruction;override;
  65.   procedure BeforeDestruction;override;
  66. end;
  67.  
  68. constructor TDescendant.create;
  69. begin
  70.  {no inherited to explain TObject create}
  71. end;
  72.  
  73. destructor TDescendant.destroy;
  74. begin
  75.   {same as above}
  76. end;
  77.  
  78.  
  79.  
  80. procedure TDescendant.AfterConstruction;
  81. begin
  82.  
  83.  writeln ('Build:',self.classname);
  84. end;
  85.  
  86. procedure TDescendant.BeforeDestruction;
  87. begin
  88.  
  89.   writeln ('Destroyed:',self.classname);
  90. end;
  91.  
  92. type
  93. TDescendant2= class (TDescendant)
  94.  
  95. end;
  96.  
  97.  
  98. var
  99.   MyClass: TObject2;
  100.   MyDescendant:TDescendant;
  101.   MyDescendant2:TDescendant2;
  102.  
  103. begin
  104.   MyClass:=TObject2.Create;
  105.   MyDescendant:=TDescendant.Create;
  106.   MyDescendant2:=TDescendant2.Create;
  107.  
  108.   writeln;
  109.   writeln ('Hi World');
  110.   writeln;
  111.  
  112.   FreeAndNil (MyClass);
  113.   FreeAndNil (MyDescendant);
  114.   FreeAndNil (MyDescendant2);
  115.   readln;
  116.  
  117. end.                                                                  
  118.  
« Last Edit: December 09, 2018, 12:47:08 pm by damieiro »