Ste*_*han 2 delphi generics delphi-10-seattle
我正在尝试使用Delphi 10 Seattle并尝试创建我的第一个Generic Container类.我需要有关Generic Comparer的帮助
这是我创建的一个简单的Hash对象:
type
TsmHeap<T> = class
private
fList: TList<T>;
Comparer: TComparer<T>;
procedure GetChildren(ParentIndex: integer; var Child1, Child2: integer);
function GetParent(ChildIndex: integer): integer;
function GetCapacity: integer;
function GetCount: integer;
function MustSwap(iParent, iChild: integer): boolean;
procedure SetCapacity(const Value: integer);
public
constructor Create(aComparer: TComparer<T>); overload;
constructor Create(aComparer: TCOmparer<T>; aCapacity: integer); overload;
destructor Destroy; override;
//-- Methods & Functions
function Dequeue: T;
procedure Enqueue(Item: T);
function IsEmpty: boolean;
//-- Properties
property Count: integer read GetCount;
property Capacity: integer read GetCapacity write SetCapacity;
end;
Run Code Online (Sandbox Code Playgroud)
我已经编写了方法的代码,它自己编译没有任何问题.但是,当我尝试创建类的整数版本时,我无法编译它.
有问题的代码是:
iHeap := TsmHeap<integer>.Create(TComparer<integer>.Construct(
function(const Left, Right: integer): integer
begin
result := Sign(Left - Right);
end)
);
Run Code Online (Sandbox Code Playgroud)
这给出了一个"E2250"没有可以用这些参数调用的'Create'的重载版本"
我究竟做错了什么?如何创建Comparer?
TComparer<T>.Construct返回IComparer<T>- 它是一个类函数而不是构造函数.只需更改TsmHeap<T>.Createto 的参数类型即可IComparer<T>.