如何声明两个相互关联的类?

Niy*_*wan 5 delphi delphi-xe2

我有一个与此类似的问题,但是在delphi中.

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

type
  TAsyncPopulator = class
  private
    _updater: TThreadPopulator;
  end;
Run Code Online (Sandbox Code Playgroud)

提到的问题的解决方案不适用于delphi

Ari*_*The 12

Forward Declarations and Mutually Dependent Classes文档.

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;
Run Code Online (Sandbox Code Playgroud)

使用来源,卢克!您的Delphi安装具有完整的VCL和RTL源,供您阅读,观看和学习.它经常使用这个模板.每次当你问自己"我怎么能做到"时,只要想想"Borland是怎么做的",而且很有可能你已经在Delphi提供的资源中得到了一个现成的例子.

  • 而不是"使用源",这个答案将通过对前向声明如何工作的解释得到改进,并且通过"前向声明必须通过同一类的定义声明来解决前提声明的含义的明确证明"相同类型的声明部分." (7认同)
  • 此代码在XE2 Update 4 Hotfix 1中编译.您的代码有些不同 - 请仔细检查.很可能你关闭一个TYPE部分并过早地启动另一个TYPE部分.前向声明和最终声明不能分开.我想你只是将旧代码与我的代码混合在一起. (2认同)