Delphi (-XE) : casting to a record type with implicit conversion

倖福魔咒の 提交于 2020-01-02 04:40:10

问题


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;

回答1:


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

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.)



来源:https://stackoverflow.com/questions/7666786/delphi-xe-casting-to-a-record-type-with-implicit-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!