具有前向声明的通用类类型约束 - delphi 2009

时间:2015-08-03 22:34:55

标签: delphi generics delphi-2009 type-constraints

在Delphi 2009中,我们可以使用类类型作为泛型类型声明的约束:

type
  TMyBaseClass = class
    //Attributes and methods here
  end;

type
  TMyGenericClass<T: TMyBaseClass> = class
    //Attributes and methods here
  end;

但是当我需要在TMyGenericClass<TMyBaseClass>内声明类型TMyBaseClass的属性时,需要一个前向声明来避免类之间的循环引用/约束关系。但在这种特殊情况下,编译器说类类型不是有效约束:

type
  TMyBaseClass = class; //forward declaration

  TMyGenericClass<T: TMyBaseClass> = class //the error is highlighted here, with [DCC Error] E2510 Type 'TMyBaseClass' is not a valid constraint
    //Attributes and methods here
  end;

  TMyBaseClass = class
  private
    FObject: TMyGenericClass<TMyBaseClass>; //This is a requirement
    //Other attributes and methods here
  end;

完成此任务的任何建议? 谢谢!

0 个答案:

没有答案
相关问题