【发布时间】:2014-10-19 12:20:37
【问题描述】:
我正在尝试在泛型中实现 TCollection 和 TCollectionItem 功能。为了解决这个问题,我们需要前向声明TGenericCollectionItem 或TGenericCollection。实际 (XE6) Delphi 编译器不支持使用泛型类型的前向声明。 There is a hack 如何制作。但是我们在将所有者分配给TCollectionItem 时仍然存在问题。
TBaseElement = class // hack for forward declaration of a generic type
end;
TGenericCollection<T: TBaseElement> = class(TObjectList<TBaseElement>)
protected
procedure Notify(const Value: TBaseElement; Action: TCollectionNotification); override;
end;
TGenericCollectionItem = class(TBaseElement)
public
Owner: TGenericCollection<TBaseElement>;
end;
procedure TGenericCollection<T>.Notify(
const Value: TBaseElement; Action: TCollectionNotification);
begin
inherited;
if (Action = cnAdded) then
begin
if (Value is TGenericCollectionItem) then
(Value as TGenericCollectionItem).Owner := Self; //here is error
end;
end;
E2010 不兼容的类型:
'TGenericCollection<TBaseElement>'和'TGenericCollection<TGenericCollection<T>.T>'
如何解决这个冲突?
【问题讨论】:
-
泛型有什么用?您有一个非通用集合项的集合。泛型不需要这样做。
标签: delphi generics collections delphi-xe6