Delphi 如何在对象类型的过程的in参数中传递nil值
我想在声明为对象的Delphi 如何在对象类型的过程的in参数中传递nil值,delphi,delphi-2007,delphi-xe,Delphi,Delphi 2007,Delphi Xe,我想在声明为对象的过程的参数中传递一个nil值 考虑一下这个代码 案例1 案例3 此外,我还发现,如果删除重载指令,代码将编译得很好 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
过程的参数中传递一个nil值
考虑一下这个代码
案例1
案例3
此外,我还发现,如果删除重载
指令,代码将编译得很好
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;
类型
TFooProc=对象的过程(Foo1,Foo2:Integer);
过程DoSomething(Param1:Integer;Foo:TFooProc);
变量
a、 b:整数;
开始
a:=b*Param1;
//如果分配了foo
如果@Foonil那么
Foo(a,b),;
结束;
过程DoSomething2(参数1:整数);
开始
剂量测定法(参数1,无);
结束;
问题是如何将nil值作为参数传递?
在案例1中使用代码?将nil类型转换为TFooProc:
DoSomething(Param1, TFooProc(nil));
感谢@Sertac的解决方案,你知道这种delphi编译器行为的原因吗?@Salvador-不完全是这样,当出现重载时,编译器会寻找更严格的匹配,即“nil”似乎是“指针”(pchar、类型等)更自然的匹配。无论如何,这看起来肯定是编译器中的一个小故障。为什么要检查@foo nil
?一个简单的Assigned(Foo)
可以避免否定,并且Assigned
通常用于检查指针和方法引用。
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;
DoSomething(Param1, TFooProc(nil));