【问题标题】:Delphi (-XE) : casting to a record type with implicit conversionDelphi (-XE):通过隐式转换转换为记录类型
【发布时间】:2011-10-05 19:41:36
【问题描述】:

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

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;

【问题讨论】:

  • 备案申报怎么样?
  • @HMcG 你真的应该给我们看完整的记录,呵呵 ;-)

标签: delphi casting delphi-xe record implicit


【解决方案1】:

是的,这是绝对安全的。代码MeasurementRecord(Edit1.text) 将使用您的

从字符串Edit1.Text 创建一个MeasurementRecord 记录
class operator Implicit(S: string): MeasurementRecord

然后在里面调用函数returnQC。 (但是,如果你也有一个

class operator Explicit(S: string): MeasurementRecord

然后将使用 this 代替,因为强制转换实际上是显式的。)

【讨论】:

  • 这对我来说似乎很明确。如果显式不可用,这是否会退回到隐式?
  • @David:是的,这正是它的作用。我只是对这一点进行了澄清。
  • 感谢您的解释。我不知道显式/隐式后备。
猜你喜欢
  • 2011-11-17
  • 2015-10-20
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多