为什么不反对默认为nil? [英] Why doesn't object default to nil?

查看:111
本文介绍了为什么不反对默认为nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi中,从TObject派生的变量的已记录行为是默认值nil。但是,我遇到的情况并非如此。

In Delp the documented behavior for variables descending from TObject is a default value of nil. However, I have come across a situation where this is not the case.

通过IDE(F9)运行以下代码示例会得出不同的结果

Running the following code sample through the IDE (F9) gives mixed results

var
  objTemp : TMemDataSet;
begin
  if (objTemp = nil) then
     ShowMessage('Nil');
end;




  • 32位/调试模式,默认不为nil

  • 32位/释放模式,默认不为nil

  • 64位/调试模式, 默认为nil

  • 64位/释放模式,默认不为nil

    • 32 Bit / Debug Mode, not defaulted to nil
    • 32 Bit / Release Mode, not defaulted to nil
    • 64 Bit / Debug Mode, is defaulted to nil
    • 64 Bit / Release Mode, not defaulted to nil
    • 我的理解是,该值应始终默认为nil。

      My understanding is the value should always default to nil.

      也在XE2和XE5上进行了测试,结果相同。

      Also tested this under XE2 and XE5 with the same results.

      这是Delphi中的预期行为吗?

      Is this the expected behavior in Delphi?

      推荐答案

      您的理解不正确。非托管类型(IOW,非引用计数类型)的局部变量未初始化。您必须先给它们分配一个值,然后才能使用它们。

      Your understanding is incorrect. Local variables to non-managed types (IOW, non reference-counted types) are not initialized. You have to assign a value to them before you can use them.

      XE5文档(请参见声明变量部分的底部-我在Wiin32中包含了键入错误,但重点是我的):

      From the XE5 documentation (see the bottom portion of the "Declaring Variables" section - I've included type typo in Wiin32, but the emphasis is mine):


      如果未显式初始化全局变量,则编译器
      会将其初始化为0。对象实例数据(字段)也将$ b​​ $ b初始化为0。在Win32平台上,本地
      变量的内容在分配值之前是不确定的。

      请注意,每当Emba写入 ; Win32它们表示非ARC编译器,因此以上内容也适用于Win64和OSX。

      您可以在Delphi 2007中找到相同的信息通过使用帮助索引中的搜索词变量;它是变量VBScript之间的一种和变量[OpenGL]。

      You can find the same information in Delphi 2007 by using the search term Variables in the help index; it's the one between "variables VBScript" and "variables [OpenGL]".

      您在Win64调试版本中看到的差异可能只是编译器所做的事情,幸运的事故或完全是其他事情。不过没关系。如您所知,局部变量默认情况下未初始化,因此只需在使用前确保在所有情况下都进行初始化。实施这条规则并不困难;当您声明局部变量时,

      The disparity you're seeing with the Win64 debug build may just be something done by the compiler, a lucky accident, or something else entirely. It shouldn't matter, though. As you know that local variables are not initialized by default, simply make sure you do so in all cases before using them. It's not a difficult rule to enforce; when you declare a local variable,

      var
        MyObj: TSomething;
      

      您是自己分配值,还是从代码中的其他地方收到的值:

      you're either assigning a value yourself, or something you've received from elsewhere in your code:

      MyObj := TSomething.Create;   // Created yourself
      MyObj := GetSomething();      // Function result
      MyObj := Self.SomethingCollection[Self.SomethingCount - 1]; // Local ref
      

      绝对没有理由需要依赖于初始化或未初始化的局部变量,因为测试可以在分配给本地变量之前在外部引用上进行,也可以在分配外部引用之后在本地变量上进行:

      There should be absolutely no reason to need to depend on a local variable being initialized or not, as the test can be done on either the external reference before assignment to the local var, or on the local var after assignment of the external reference:

      if SomethingIGot = nil then
        raise Exception.Create('Received a nil parameter');
      MyObj := SomethingIGot;
      
      // or
      
      MyObj := SomethingIGot;
      if not Assigned(MyObj) then
        raise Exception.Create('MyObj was assigned a nil value');
      

      这篇关于为什么不反对默认为nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆