Pet*_*ner 5 delphi operator-overloading delphi-2009
作为Delphi 7转换的一部分,我们摆脱了shorttring.我想让它尽可能轻松,所以我们认为我们可以将ShortString更改为一些以相同方式起作用的记录.这是它的声明方式(还有更多内容,但这是基本结构,概述了问题):
TShortStringRec = record
private
FStuff: array [0..49] of Char;
public
class operator Implicit(AStuff: TShortStringRec): String;
class operator Implicit(S1: String): TShortStringRec;
end;
Run Code Online (Sandbox Code Playgroud)
这适用于将字符串设置为记录.但是那时的功能就像format参数一样const array of const.有没有办法对我们想要传递给const数组的内容进行隐含转换?
function FunkyFunc : string;
var
ssr : TShortStringRec;
begin
ssr := 'Wall';
result := format('Hello %s', [ssr]); //<---error here
end;
Run Code Online (Sandbox Code Playgroud)
编译时出现语法错误,因为ssr不是可以在其中一个数组上使用的参数类型.
简答:不.
答案很长:你要求的是编译器以某种方式知道你想要一个固有的无类型参数被强制转换成你想要的类型.编译器在调用站点上没有足够的信息来进行确定.如果添加"Explicit"运算符然后显式地将参数转换为字符串,那么它将起作用.