Delphi(-XE):使用隐式转换转换为记录类型

HMc*_*McG 5 delphi casting record implicit delphi-xe

我有一个带有方法的记录类型,表示特定的硬件测量类型,从仪器读取为字符串.该记录包含对字符串的隐式转换(以及来自).如果我把一个字符串作为记录类型,它似乎工作,但这样安全吗?也就是说,将字符串转换为具有隐式字符串转换的记录是否会根据分配临时值调用隐式转换?

var  a: MeasurementRecord;         // record type with implicit string conversion & decode methods
b: string;
c:double;
begin
b := Edit1.Text;              // Or any other string source 
a:=b;                         //Ok
a:= edit1.text;               //Ok
c:= a.returnQc;                 // returns measurement quality value

c:= MeasurementRecord(Edit1.text).returnQC;   //Avoiding local variable. This works, but is it correct useage?

end;
Run Code Online (Sandbox Code Playgroud)

And*_*and 9

是的,这是非常安全的.代码MeasurementRecord(Edit1.text)将使用您MeasurementRecord的字符串从字符串创建记录Edit1.Text

class operator Implicit(S: string): MeasurementRecord
Run Code Online (Sandbox Code Playgroud)

然后调用其中的函数returnQC.(但是,如果你也有

class operator Explicit(S: string): MeasurementRecord
Run Code Online (Sandbox Code Playgroud)

然后这将被使用,因为演员实际上是明确的.)

  • @David:是的,这正是它的作用.我刚才补充说明了这一点. (2认同)