delphi – 是否有一种解决方法可以使类操作符适用于内置类型

我可以使用类操作符来使用中间保持记录.
这样我就可以隐式转换内置类型了.

program TestNewStringHelper; 
{$APPTYPE CONSOLE}
uses
  System.SysUtils;
type
  TStringRecord = record
  private
    Data: string;
  public
    class operator Implicit(const a: TStringRecord): Integer; inline;
    class operator Implicit(const a: string): TStringRecord; inline;
  end;

{ TStringRecord }

class operator TStringRecord.Implicit(const a: string): TStringRecord;
begin
  pointer(Result.Data):= pointer(a);
end;

class operator TStringRecord.Implicit(const a: TStringRecord): Integer;
begin
  Result:= StrToInt(a.Data);
end;

var
  input: TStringRecord;
  output: integer;

begin
  input:= '42';
  output:= input;
  WriteLn(IntToStr(output));
  ReadLn;
end.

我想做一些相反的事情:

var
  input: string;
  output: integer;

begin
  input:= '42';
  output:= input;  //Class operator magic
  WriteLn(IntToStr(output));
  ReadLn;
end.

根据在线帮助:

Note: Class and record helpers do not support operator overloading.

http://qc.embarcadero.com/wc/qcmain.aspx?d=72253

是否有解决方法来实现内置类型的隐式转换而不使用中间类型?

解决方法

记录和类助手是扩展现有类型的方法范围的唯一方法.并且助手不承认操作符超载.

您可以定义重载运算符的唯一方面是定义类型.

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...