delphi – 引用重载函数(或过程)
发布时间:2020-12-15 09:52:22 所属栏目:大数据 来源:网络整理
导读:我用的是这种类型 type TRealFunction = reference to function(const X: extended): extended; 在我的代码中很多.假设我有一个变量 var rfcn: TRealFunction; 并尝试为其分配Math.ArcSec: rfcn := ArcSec; 这与Delphi 2009中的预期一样,但现在我尝试在Delp
我用的是这种类型
type TRealFunction = reference to function(const X: extended): extended; 在我的代码中很多.假设我有一个变量 var rfcn: TRealFunction; 并尝试为其分配Math.ArcSec: rfcn := ArcSec; 这与Delphi 2009中的预期一样,但现在我尝试在Delphi 10.2中编译它,并且编译器感到不安: [dcc32 Error] Unit1.pas(42): E2010 Incompatible types: 'TRealFunction' and 'ArcSec' 看起来,差异在于ArcSec在Delphi 10.2中过载:它有单一,双重和扩展的风格.看起来编译器不喜欢引用这种类型的重载函数(或过程)(太类似的类型?). 但是,如果我重新定义 type TRealFunction = function(const X: extended): extended; 它编译得很好. 当然,这里有明显的解决方法:我可以定义 function ArcSec(const X: extended): extended; inline; begin result := Math.ArcSec(X); end; 或者我可以写 rfcn := function(const X: extended): extended begin result := Math.ArcSec(x); end; 尽管如此,这仍然需要编写很多代码.有更简单的解决方法吗? 解决方法
这有效:
type TRealFunction = function(const X: extended): extended; const rc : TRealFunction = Math.ArcSec; type TRealFunctionRef = reference to function(const X: Extended) : Extended; var rfcn: TRealFunctionRef; begin rfcn := rc; ... 它需要一个额外的类型声明,但也许值得努力. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |