delphi – 如何为相互依赖的记录定义隐式转换操作符?

我在Delphi 2006中使用操作符重载记录(请不要通过告诉我不要回答这个问题)

我有两个记录类型,隐式运算符重载.它们都只是在实现模块,而不是通过界面暴露出来.

我的问题是,现在他们是相互依赖的,我不知道如何将第二个类型声明为编译器.我知道如何使用函数,过程和类来实现,而不是记录.

以下是我正在尝试做的简化示例:

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;

解决方法

您不能有记录类型的转发声明.在第二个类型中定义两个隐式运算符:
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 API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...