delphi - 如何为相互依赖的记录定义隐式转换运算符?

标签 delphi delphi-2006

我在 Delphi 2006 中对记录使用运算符重载。(请不要告诉我不要回答这个问题。)

我有两种记录类型,其中隐式运算符重载。它们都只在模块的实现中,不通过接口(interface)暴露。

我的问题是,既然它们是相互依赖的,我不知道如何将第二种类型的声明转发给编译器。我知道如何处理函数、过程和类,但不知道如何处理记录。

这是我正在尝试做的一个简化示例:

implementation

type
  TMyRec1 = record
    Field1 : Integer;
    class operator Implicit(a: TMyRec2): TMyRec1;  // <---- Undeclared Identifier here.
  end;

  TMyRec2 = record
    Field2: Integer;
    class operator Implicit(a: TMyRec1): TMyRec2;
  end;

class operator TMyRec1.Implicit(a:TMyRec2): TMyRec1;
begin
  Result.Field1 := a.Field2;
end;

class operator TMyRec2.Implicit(a:TMyRec2): TMyRec2;
begin
  Result.Field2 := a.Field1;
end;

最佳答案

您不能有记录类型的前向声明。定义两个 Implicit第二种类型的运算符:

type
  TMyRec1 = record
    Field1 : Integer;
  end;

  TMyRec2 = record
    Field2: Integer;
    class operator Implicit(a: TMyRec2): TMyRec1;
    class operator Implicit(a: TMyRec1): TMyRec2;
  end;

引自 the help :

Implicit conversions should be provided only where absolutely necessary, and reflexivity should be avoided. It is best to let type B implicitly convert itself to type A, and let type A have no knowledge of type B (or vice versa).

关于delphi - 如何为相互依赖的记录定义隐式转换运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43206352/

相关文章:

delphi - Delphi中查找编号最大的文件的有效方法

delphi - 使用 ASM 调用对象方法

delphi - 等待 Thread 实例启动的正确方法是什么

web-services - 如何从 Web 服务返回异常?

Delphi SelectClipRGN 隐藏绘制位图

delphi - 使用 XML 数据绑定(bind)时生成复杂类型的问题

Delphi - 当表单的 ComponentCount 递减时如何中断

delphi - BcdDivide 函数在 Delphi XE 和 XE2 中的行为不同

delphi - 为什么这个用于淡化 PNG 图像的 D2006 代码不起作用?

Delphi XE2 FormatDateTime 传递 -693594