7

我有一个与此类似的问题但在 delphi 中。

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

type
  TAsyncPopulator = class
  private
    _updater: TThreadPopulator;
  end;

上述问题的解决方案不适用于delphi

4

3 回答 3

13

请参阅Forward Declarations and Mutually Dependent Classes文档。

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. *)
  ....

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

于 2012-10-22T06:09:50.847 回答
3

在任何类定义之前使用它。Forward 类在 Delphi 2010 中有效。我不知道你拥有的 delphi 女巫版本,但这是我能想到的唯一解决方案。

type   
 TAsyncPopulator = Class;

希望我有所帮助

于 2012-10-22T06:07:08.743 回答
1

除了使用前向声明,您还可以创建一个子类来解决这个问题:

TThreadPopulator = class(TThread)
  type 
    TAsyncPopulator = class 
      _updater: TThreadPopulator;  
    end;

  var 
    owner: TAsyncPopulator;
end;
于 2013-03-04T23:47:41.410 回答