已分配vs零 [英] Assigned vs <> nil
本文介绍了已分配vs零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果分配(Foo)和 If(Foo<> nil)
如果是的话,应该何时使用它们?
Is there any difference between If Assigned(Foo)
and If (Foo <> nil)
? If So, when should they each be used?
推荐答案
几乎是一样的。 官方文档指出
$ b $分配(P)对应于指针变量的测试
P< nil,程序变量的@P
Assigned(P) corresponds to the test P<> nil for a pointer variable, and @P <> nil for a procedural variable.
所以,如果 P
是一个普通指针,那么 P < nil
和 Assigned(P)
等同于。另一方面,如果 P
是一些程序,那么
So, if P
is an ordinary pointer, then P <> nil
and Assigned(P)
are exactly equivalent. On the other hand, if P
is some procedure, then
var
p: TNotifyEvent = nil;
procedure TForm1.FormCreate(Sender: TObject);
begin
if Assigned(p) then
p(Self);
end;
将会正常工作,
procedure TForm1.FormCreate(Sender: TObject);
begin
if @p <> nil then
p(Self);
end;
但
procedure TForm1.FormCreate(Sender: TObject);
begin
if p <> nil then
p(Self);
end;
甚至不会编译。因此,结论是 P < nil
和 Assigned(P)
在每次都工作时完全相同!
will not even compile. Hence, the conclusion is that P <> nil
and Assigned(P)
are exactly equivalent every time both works!
这篇关于已分配vs零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文