How to declare two inter-linked classes?

See Forward Declarations and Mutually Dependent Classes documentation.

type (* start type section - one unified section "to rule them all" *)

  TAsyncPopulator = class; (* forward declaration, it basically serves just to
  fix SizeOf(TAsyncPopulator) so compiler would be able to draft dependent types'
  in-memory layout. Down the CPU level `class` and `pointer` is the same, so now 
  Delphi knows SizeOf(TAsyncPopulator) = SizeOf(pointer) to compose further types *)

  TThreadPopulator = class(TThread)
  private
    _owner:TASyncPopulator;
  end;
    
  TAsyncPopulator = class (* the final declaration now, it should go WITHIN 
  that very TYPE-section where the forward declaration was made! *)
  private
    _updater: TThreadPopulator;
  end;

  ....

type 
(* now, that you had BOTH forward and final declarations for TAsyncPopulator 
   spelled out above, you finally may start another TYPE-section, if you choose so. 
   But only after them both, and never should a new `type` section be inserted 
   in between those declarations. *)
  ....

Use the source, Luke! Your Delphi installation has full VCL and RTL sources for you to read and watch and learn. And it uses this template a lot. Every time when you ask yourself “how I could do it”, just think along “how did Borland do it” and pretty chance that you can already get a ready-made example in Delphi-provided sources.

Leave a Comment