英文:
How to make a function point to another function (with overload)?
问题
以下是代码部分的中文翻译:
function ALmyfunctA(const Filename: AnsiString): Boolean;
function ALmyfunctW(const Filename: String): Boolean;
function ALmyfunct(const Filename: AnsiString): Boolean; overload;
function ALmyfunct(const Filename: String): Boolean; overload;
英文:
I have these 4 functions:
function ALmyfunctA(const Filename: AnsiString): Boolean;
function ALmyfunctW(const Filename: String): Boolean;
function ALmyfunct(const Filename: AnsiString): Boolean; overload;
function ALmyfunct(const Filename: String): Boolean; overload;
Is their an easy way to do something like this?
function ALmyfunct(const Filename: AnsiString): Boolean; = function ALmyfunctA(const Filename: AnsiString): Boolean;
and
function ALmyfunct(const Filename: String): Boolean; = function ALmyfunctA(const Filename: String): Boolean;
I could do it using var
:
var ALmyfunct: function(const Filename: String): Boolean;
But it does not work with the overload
So, is there a way to do this?
答案1
得分: 5
FUNCTION AlMyFunct(CONST FileName : STRING) : BOOLEAN; INLINE;
BEGIN
Result:=AlMyFunctA(FileName)
END;
通过声明它为INLINE,您不会产生额外的调用。调用将直接转向A函数,而不会到达非A版本。
英文:
FUNCTION AlMyFunct(CONST FileName : STRING) : BOOLEAN; INLINE;
BEGIN
Result:=AlMyFunctA(FileName)
END;
By declaring it INLINE you don't incur an additional call. The call is diverted directly to the A function without ever reaching the non-A version.
答案2
得分: 3
无法将一个函数分配给另一个函数。但你可以从一个函数内部调用另一个函数。
但你确实可以将重载函数分配给一个函数变量:
function ALmyfunctA(const Filename: AnsiString): Boolean;
begin
end;
function ALmyfunctW(const Filename: string): Boolean;
begin
end;
function ALmyfunct(const Filename: AnsiString): Boolean; overload;
begin
end;
function ALmyfunct(const Filename: string): Boolean; overload;
begin
end;
procedure Test;
var
F1: function(const Filename: string): Boolean;
F2: function(const Filename: AnsiString): Boolean;
begin
// 这个有效
F1 := AlmyfunctW;
F2 := AlmyfunctA;
// 这也有效
F1 := Almyfunct;
F2 := Almyfunct;
end;
注意:我建议使用类型:
type
TStringToBool = function(const Filename: string): Boolean;
TAnsiStringToBool = function(const Filename: AnsiString): Boolean;
var
F1: TStringToBool;
F2: TAnsiStringToBool;
在Delphi 10.4中尝试的代码。
英文:
You can not assign a function to another function. Although you can call a function from within another function.
But you definitely can assign overloaded functions to a function variable:
function ALmyfunctA(const Filename: AnsiString): Boolean;
begin
end;
function ALmyfunctW(const Filename: string): Boolean;
begin
end;
function ALmyfunct(const Filename: AnsiString): Boolean; overload;
begin
end;
function ALmyfunct(const Filename: string): Boolean; overload;
begin
end;
procedure Test;
var
F1: function(const Filename: string): Boolean;
F2: function(const Filename: AnsiString): Boolean;
begin
// This works
F1 := AlmyfunctW;
F2 := AlmyfunctA;
// This works Too
F1 := Almyfunct;
F2 := Almyfunct;
end;
Note: I recommend using types:
type
TStringToBool = function(const Filename: string): Boolean;
TAnsiStringToBool = function(const Filename: AnsiString): Boolean;
var
F1: TStringToBool;
F2: TAnsiStringToBool;
Code tried in Delphi 10.4.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论