【发布时间】:2009-02-24 01:17:13
【问题描述】:
我正在尝试创建一个通用列表类以与 tiOPF(delphi @ www.tiopf.com 的对象持久性框架)一起使用。具体来说,我正在尝试采用现有的通用类(TtiObjectList)并制作一个使用 TtiObject 后代的通用版本。
我更改基类的范围有限,因为它们需要在 D7 - D2009 和 Free Pascal 下编译。我需要从 TtiObjectList 降级以保持现有的持久性机制正常工作。
// base class
type
TtiObjectList = class(TtiObject)
...
protected
function GetItems(i: integer): TtiObject; virtual;
procedure SetItems(i: integer; const AValue: TtiObject); virtual;
...
public
function Add(const AObject : TtiObject): integer; overload; virtual;
...
end;
我的类定义如下:
TtiGenericObjectList<T: TtiObject> = class(TtiObjectList)
protected
function GetItems(i:integer): T; reintroduce;
procedure SetItems(i:integer; const Value: T); reintroduce;
public
function Add(const AObject: T): integer; reintroduce;
property Items[i:integer]: T read GetItems write SetItems; default;
end;
implementation
{ TtiGenericObjectList<T> }
function TtiGenericObjectList<T>.Add(const AObject: T): integer;
var obj: TtiObject;
begin
obj:= TtiObject(AObject); /// Invalid typecast
result:= inherited Add(obj);
end;
// alternate add, also fails
function TtiGenericObjectList<T>.Add(const AObject: T): integer;
begin
result:= inherited Add(AObject); /// **There is no overloaded version**
/// **of 'Add' that can be called with these arguments**
end;
function TtiGenericObjectList<T>.GetItems(i: integer): T;
begin
result:= T(inherited GetItems(i)); /// **Invalid typecast **
end;
procedure TtiGenericObjectList<T>.SetItems(i: integer; const Value: T);
begin
inherited SetItems(i, Value);
end;
我遇到的问题是 delphi 没有将 T 视为 TtiObject 后代。当我执行以下操作时,我收到了无效的类型转换错误:
function TtiGenericObjectList<T>.Add(const AObject: T): integer;
var obj: TtiObject;
begin
obj:= TtiObject(AObject); /// **Invalid typecast***
result:= inherited Add(obj);
end;
如果我不进行类型转换,则会收到重载错误,如上面的清单所示。
任何想法我哪里出错了?
肖恩
【问题讨论】:
-
这是 Delphi 2009 编译器中的一个错误;我认为它已在 Delphi 2010 编译器中解决。
标签: delphi generics delphi-2009