assigned函数有什么用

解决方案 »

  1.   

    if Assigned(Form1) then

    if Form1 <> nil then
    等价
      

  2.   

    一般可用 = nil 来判断取代Assigned不过有时候不行,比如,LoadLibrary一个DLL并GetProcAddress取得一个函数指针后,要判断该函数指针是否有效,就只能用Assigned来判断,如果用=nil来判断,编译器会认为判断的是函数的返回值是否为nil。
      

  3.   

    if Assigned(Form1) then

    if Form1 <> nil then
    等价我认为不等价,原因
    Assigned是判断指针所指向的内容是否为空。如果Form1指向的内容(即对象)被释放,但没有把Form1赋成nil的话,则会出现下列的情况:
       Assigned(Form1)返回false
       而Form1 <> nil返回true
    可见是不等价的,如果Form1是一个全局变量的话,释放Form1用Form1.free是不严格的,应该用FreeAndNil(Form1);
      

  4.   

    Note: Assigned can't detect a dangling pointer--that is, one that isn't nil but no longer points to valid data. For example, in the code example for Assigned, Assigned won't detect the fact that P isn't valid.