gpt4 book ai didi

delphi - 如何在对象类型过程的 in 参数中传递 nil 值

转载 作者:行者123 更新时间:2023-12-03 14:51:25 25 4
gpt4 key购买 nike

我想在声明为对象过程的参数中传递一个nil值

考虑这段代码

案例1

type
TFooProc = procedure(Foo1, Foo2 : Integer) of object;


procedure DoSomething(Param1:Integer;Foo:TFooProc);overload;
var
a, b : Integer;
begin
a:=b*Param1;
//If foo is assigned
if @Foo<>nil then
Foo(a, b);
end;

procedure DoSomething(Param1:Integer);overload;
begin
DoSomething(Param1,nil);//here the delphi compiler raise this message [DCC Error] E2250 There is no overloaded version of 'DoSomething' that can be called with these arguments
end;

案例2

发现,如果我将 TFooProc 声明为 procedure 类型,则会编译代码。 (但就我而言,我需要一个对象过程类型)

type
TFooProc = procedure(Foo1, Foo2 : Integer);


procedure DoSomething(Param1:Integer;Foo:TFooProc);overload;
var
a, b : Integer;
begin
a:=b*Param1;
//If foo is assigned
if @Foo<>nil then
Foo(a, b);
end;

procedure DoSomething(Param1:Integer);overload;
begin
DoSomething(Param1,nil);
end;

案例3

我还发现,如果删除 overload 指令,代码可以正常编译

type
TFooProc = procedure(Foo1, Foo2 : Integer) of object;


procedure DoSomething(Param1:Integer;Foo:TFooProc);
var
a, b : Integer;
begin
a:=b*Param1;
//If foo is assigned
if @Foo<>nil then
Foo(a, b);
end;

procedure DoSomething2(Param1:Integer);
begin
DoSomething(Param1,nil);
end;

问题是我如何将 nil 值作为参数传递?以使用案例 1 中的代码?

最佳答案

将 nil 类型转换为 TFooProc:

DoSomething(Param1, TFooProc(nil));

关于delphi - 如何在对象类型过程的 in 参数中传递 nil 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6603111/

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