我有这样的课程:
type A = class
procedure<T> DoStuff(tPtr: ^T);
end;
但是当我尝试编译时,Delphi给了我这个错误:
[DCC Error] RPML.pas(57): E2029 Identifier expected but '^' found
如何在Delphi过程中使用指向参数化类型的指针?我不想让整个类都成为模板类。
为此,您需要在通用类中将指针类型声明为嵌套类型:
type
TMyGeneric<T> = class
type
P = ^T;
public
procedure DoStuff(tPtr: P);
end;
并且如果您想要一个类方法(即不是实例方法),您可以用这种方法:
type
TMyGeneric<T> = record
type
P = ^T;
public
class procedure DoStuff(tPtr: P); static;
end;
var
int: Integer;
...
TMyGeneric<Integer>.DoStuff(@int);
或使用var参数:
type
TMyGeneric<T> = record
public
class procedure DoStuff(var a: T); static;
end;
对于从未实例化的泛型类型,使用记录而不是类似乎很常见。
最后,在Delphi中,如果不使类成为泛型,就无法拥有泛型方法。换句话说,以下C ++模板代码没有类似物:
Thorsten的答案显示了如何在不使类通用的情况下实现通用方法,即以下C ++模板代码的Delphi类似物:
class C {
public:
template <typename T>
int SomeTemplateFunction(T* data) {
printf("Address of parameter is %p\n", data);
return 0;
}
};
int a;
char c;
C cinst;
cinst.SomeTemplateFunction<int>(&a);
cinst.SomeTemplateFunction<char>(&c);
索尔斯顿的答案为您提供了一个类函数,但在注释中指出您正在寻找普通的成员函数。
type
TMyClass = class
public
procedure DoStuff<T>(var a: T);
end;
procedure TMyClass.DoStuff<T>(var a: T);
begin
end;
...
var
instance: TMyClass;
i: Integer;
s: string;
...
instance.DoStuff<Integer>(i);
instance.DoStuff<string>(s);
但是,我正在苦苦挣扎的是,在Delphi中,您如何才能做到这一点非常有用的事情,如果没有通用的解决方案,这将无法同样有效地完成。
我将不胜感激任何建议,很高兴编辑答案以适应他们。
您可以将通用参数从类移至方法,并使用var代替指针类型:
type
TMyGeneric = record
class procedure DoStuff<T>(var aParam: T); static;
end;
var
int : Integer;
s : string;
...
TMyGeneric.DoStuff<Integer>(int);
TMyGeneric.DoStuff<string>(s);
编辑:不幸的是,当使用var参数时,Delphi编译器似乎无法执行类型推断,这使得必须在方法调用中使用<..>显式指定泛型类型。
没有“ var”,可以省略<..>(但是该方法无法再修改传入的变量)。
type
Pointer<T> = record
public type
Ty = ^T;
end;
现在您可以在任何地方使用此通用指针
type A = class
procedure<T> DoStuff(tPtr: Pointer<T>.Ty);
end;