在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;
完成此任务的任何建议? 谢谢!