Delphi (-XE) : casting to a record type with implicit conversion
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 o开发者_运维百科ther 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;
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.)
精彩评论