Delphi(-XE):转换为带有隐式转换的记录类型 [英] Delphi (-XE) : casting to a record type with implicit conversion

查看:484
本文介绍了Delphi(-XE):转换为带有隐式转换的记录类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a record type with methods, representing an specific hardware measurement type, read from the instrument as a string. The record contains implicit coversion to (and from) a string. If I cast a string as a record type, it seems to work, but is this safe? That is, does casting a string to a record with implicit string conversion call the implicit conversion as per assigning a temporary value?

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;


推荐答案

是的,这是完全安全的。代码 MeasurementRecord(Edit1.text)将从字符串 Edit1创建 MeasurementRecord 记录。文本使用您的

Yes, this is perfectly safe. The code MeasurementRecord(Edit1.text) will create a MeasurementRecord record from the string Edit1.Text using your

class operator Implicit(S: string): MeasurementRecord

然后调用 returnQC 中的函数。 (但是,如果你还有一个

and then call the function returnQC in it. (However, if you also have a

class operator Explicit(S: string): MeasurementRecord

那么这将被使用,因为强制转换实际上是显式的。)

then this will be used instead since the cast is actually explicit.)

这篇关于Delphi(-XE):转换为带有隐式转换的记录类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆