我可以使用类操作符来使用中间保持记录.
这样我就可以隐式转换内置类型了.
这样我就可以隐式转换内置类型了.
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
是否有解决方法来实现内置类型的隐式转换而不使用中间类型?