How to declare two inter-linked classes?

前端 未结 3 1274
暗喜
暗喜 2020-12-04 01:46

I have a question similar to this, but in delphi.

type
  TThreadPopulator = class(TThread)
  private
    _owner:TASyncPopulator; //Undeclared identifier
  en         


        
相关标签:
3条回答
  • 2020-12-04 02:31

    Besides using a forward declaration, you can also create a subclass to solve this:

    TThreadPopulator = class(TThread)
      type 
        TAsyncPopulator = class 
          _updater: TThreadPopulator;  
        end;
    
      var 
        owner: TAsyncPopulator;
    end;
    
    0 讨论(0)
  • 2020-12-04 02:38

    Use this before any class definition. Forward class works in Delphi 2010. I don't know witch version of delphi you have but it's the only solution I can think off.

    type   
     TAsyncPopulator = Class;
    

    Hope I helped

    0 讨论(0)
  • 2020-12-04 02:43

    See Forward Declarations and Mutually Dependent Classes documentation.

    type (* start type section - one unified section "to rule them all" *)
      TAsyncPopulator = class; (* forward declaration *)
    
      TThreadPopulator = class(TThread)
      private
        _owner:TASyncPopulator;
      end;
    
      TAsyncPopulator = class (* final declaration - WITHIN that very section where forward declaration was made *)
      private
        _updater: TThreadPopulator;
      end;
    

    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.

    0 讨论(0)
提交回复
热议问题