我怎样才能声明一个基于泛型类型的指针? [英] How can I declare a pointer based on a generic type?
问题描述
type A = class
procedure< T> DoStuff(tPtr:^ T);
end;
但是当我尝试编译时,Delphi给了我这个错误:
[DCC Error] RPML.pas(57):E2029标识符预期,但找到'^'
如何在Delphi过程中使用指向参数化类型的指针?我不想让整个类都成为一个模板类。
为此,您需要声明一个指针类型为通用类中的嵌套类型:
类型
TMyGeneric< T> = class
type
P = ^ T;
public
程序DoStuff(tPtr:P);
end;
如果你想要一个类方法(即不是实例方法),你可以这样做:
类型
TMyGeneric< T> =记录
类型
P = ^ T;
public
类程序DoStuff(tPtr:P);静态的;
end;
var
int:Integer;
...
TMyGeneric< Integer> .DoStuff(@int);
或者使用var参数:
键入
TMyGeneric< T> =记录
public
类程序DoStuff(var a:T);静态的;
end;
对于通用类型,使用记录而不是类是常见的, 。
最后,在Delphi中,不能使泛型类成为通用方法。换句话说,没有类似于以下C ++模板代码:
Thorsten的答案显示了如何在不使类通用的情况下实现泛型方法,即以下C ++模板代码的Delphi模拟:
class C {
public:
template< ; typename T>
int SomeTemplateFunction(T * data){
printf(参数的地址是%p \ n,data);
返回0;
}
};
int a;
char c;
C cinst;
cinst.SomeTemplateFunction< int>(& a);
cinst.SomeTemplateFunction< char>(& c);
Thorsten的答案为您提供了一个类函数,但在您声明的注释中您正在寻找一个正常的成员函数
类型
TMyClass = class
public
过程DoStuff< T>(var a :T);
end;
procedure TMyClass.DoStuff< T>(var a:T);
begin
end;
...
var
实例:TMyClass;
我:整数;
s:string;
...
instance.DoStuff< Integer>(i);
instance.DoStuff< string>(s);
然而,我正在努力的是如何做到这一点非常有用,德尔福,如果没有一个通用的解决方案,就无法做到这一点。
我很感激任何建议,并且很乐意编辑答案以适应它们。 p>
I have a class like this:
type A = class
procedure<T> DoStuff(tPtr: ^T);
end;
But when I try to compile, Delphi gives me this error:
[DCC Error] RPML.pas(57): E2029 Identifier expected but '^' found
How can I use a pointer to a parameterized type in a Delphi procedure? I don't want to make the whole class a template class.
To do this you need to declare a pointer type as a nested type in the generic class:
type
TMyGeneric<T> = class
type
P = ^T;
public
procedure DoStuff(tPtr: P);
end;
And if you want a class method (i.e. not an instance method) you can do it this way:
type
TMyGeneric<T> = record
type
P = ^T;
public
class procedure DoStuff(tPtr: P); static;
end;
var
int: Integer;
...
TMyGeneric<Integer>.DoStuff(@int);
Or using a var parameter:
type
TMyGeneric<T> = record
public
class procedure DoStuff(var a: T); static;
end;
It seems to be common to use records rather than classes for generic types that don't ever get instantiated.
Finally, you cannot have, in Delphi, a generic method without making the class generic. In other words there is no analogue of the following C++ template code:
Thorsten's answer shows how to implement a generic method without making the class generic, that is the Delphi analogue of of the following C++ template code:
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);
Thorsten's answer gives you a class function but in the comments you state you are looking for a normal member function.
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);
However, what I'm struggling with is how exactly you could do anything very useful with this, in Delphi, that could not be done just as effectively without a generic solution.
I'd appreciate any suggestions and would be happy to edit the answer to accommodate them.
这篇关于我怎样才能声明一个基于泛型类型的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!