如何将多个不同的记录(由于delphi限制而不是类)传递给函数?
由于 Delphi 的限制,我有许多记录无法转换为类(所有记录都使用类运算符来实现比较)。但我必须通过将它们存储在一个不知道我正在使用哪种记录类型的类中。
像这样的事情:
type R1 = record
begin
x :Mytype;
class operator Equal(a,b:R1)
end;
type R2 = record
begin
y :Mytype;
class operator Equal(a,b:R2)
end;
type Rn = record
begin
z :Mytype;
class operator Equal(a,b:Rn)
end;
type TC = class
begin
x : TObject;
y : Mytype;
function payload (n:TObject)
end;
function TC.payload(n:TObject)
begin
x := n;
end;
program:
c : TC;
x : R1;
y : R2;
...
c := TC.Create():
n:=TOBject(x);
c.payload(n);
现在,Delphi 不接受从记录到 TObject 的类型转换,并且由于 Delphi 的限制,我无法将它们创建为类。
任何人都知道如何将不同的记录传递给函数并在需要时识别它们的类型,就像我们对类所做的那样:
if x is TMyClass then TMyClass(x) ...
???
I have a number of records I cannot convert to classes due to Delphi limitation (all of them uses class operators to implement comparisons). But I have to pass to store them in a class not knowing which record type I'm using.
Something like this:
type R1 = record
begin
x :Mytype;
class operator Equal(a,b:R1)
end;
type R2 = record
begin
y :Mytype;
class operator Equal(a,b:R2)
end;
type Rn = record
begin
z :Mytype;
class operator Equal(a,b:Rn)
end;
type TC = class
begin
x : TObject;
y : Mytype;
function payload (n:TObject)
end;
function TC.payload(n:TObject)
begin
x := n;
end;
program:
c : TC;
x : R1;
y : R2;
...
c := TC.Create():
n:=TOBject(x);
c.payload(n);
Now, Delphi do not accept typecast from record to TObject, and I cannot make them classes due to Delphi limitation.
Anyone knows a way to pass different records to a function and recognize their type when needed, as we do with class:
if x is TMyClass then TMyClass(x) ...
???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您不能将不同的记录传递给单个函数,但您可以创建多个重载函数。就像这样:
这能解决你的问题吗?
I don't think you can pass different records to a single function, but you can make several overloaded functions. Like so:
Will that solve your problem?
您可以将它们转换为类,只需使用虚拟的“IsEqual”函数对“Equal”运算符进行反混淆即可。它将简化您的问题并极大地阐明您的代码。
You can convert these to classes, you simply have to de-obfuscate the "Equal" operator with a virtual "IsEqual" function. It will simplify your problem and clarify your code enormously.
记录没有运行时类型信息,这是您检测其类型所需的信息。
除了重载之外,你还可以传递类型,你可以这样做:
或者你可以这样做,但鸽子会哭泣:
Records don't have Run Time Type Information, which is what you'd need to detect their types.
Besides overloads, you can also pass the type, you can do this:
or you could do this, but doves will cry:
如果您使用的是 Delphi 2010,您可能可以传递指向这些记录的指针,并使用运行时类型信息来检测实际传递的是哪一个。但这将是非常难看的代码,而且可能也很慢。
转换为类并使用函数调用而不是运算符重载会更具可读性。
If you are using Delphi 2010, you could probably pass around pointers to these records and use runtime type information to detect which one was actually passed. But this would be very ugly code and probably quite slow as well.
Converting to classes and using function calls rather than operator overloading would be much more readable.