对重载函数(或过程)的引用
Reference to overloaded function (or procedure)
我用的类型
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 中过载:它有 single
、double
和 extended
风格.似乎编译器不喜欢对这种重载函数(或过程)的引用(类型太相似?)。
但是,如果我重新定义
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;
...
它需要额外的类型声明,但也许值得付出努力。
我用的类型
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 中过载:它有 single
、double
和 extended
风格.似乎编译器不喜欢对这种重载函数(或过程)的引用(类型太相似?)。
但是,如果我重新定义
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;
...
它需要额外的类型声明,但也许值得付出努力。