delphi记录之间的交叉引用 [英] Cross-reference between delphi records

查看:93
本文介绍了delphi记录之间的交叉引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个记录TQuaternion和一个记录TVector。四元数具有一些使用TVector参数的方法。另一方面,TVector支持某些具有TQuaternion参数的操作。

Let's say I have a record TQuaternion and a record TVector. Quaternions have some methods with TVector parameters. On the other hand, TVector supports some operations that have TQuaternion parameters.

知道Delphi(Win32)不允许进行前向记录声明,我该如何优雅地解决这个问题?

Knowing that Delphi (Win32) does not allow for forward record declarations, how do I solve this elegantly?

在这里使用类并不是真正的选择,因为在这种罕见的情况下,我真的想使用操作符重载,但这实际上是很有意义的。

Using classes is not really an option here, because I really want to use operator overloading for this rare case where it actually makes good sense.

现在,我只是将这些特殊方法从记录中移出,并移到了单独的函数中,这是一种很好的老式方法。

For now I simply moved these particular methods out of the records and into separate functions, the good old-fashioned way. Better suggestions are most welcome.

推荐答案

如果操作员不是实际问题,则可以使用记录助手来解决。

If the operators are not the actual problem you can solve this using a record helper.

type
  TVector = record
  end;

  TQuaternion = record
    procedure UseVector(var V: TVector);
  end;

  TVectorHelper = record helper for TVector
    procedure UseQuaternion(var Q: TQuaternion);
  end;

这只会解决循环依赖关系,不适用于运算符。它还有一个缺点,就是您不能为TVector拥有任何其他记录助手,至少它们两者不能在同一位置使用。

This will solve only the circular dependencies and it does not work with operators. It also has the drawback that you cannot have any other record helper for TVector, at least not both of them can be available in the same place.

这篇关于delphi记录之间的交叉引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆