gpt4 book ai didi

delphi - 对重载函数(或过程)的引用

转载 作者:行者123 更新时间:2023-12-03 15:01:13 26 4
gpt4 key购买 nike

我使用的类型

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

它需要额外的类型声明,但也许值得付出努力。

关于delphi - 对重载函数(或过程)的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48356682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com