• Delphi会自动初始化全局变量和类成员变量,但不初始化局部变量


    If you don't explicitly initialize a global variable, the compiler initializes it to 0. Object instance data (fields) are also initialized to 0. On the Wiin32 platform, the contents of a local variable are undefined until a value is assigned to them.

    所以只有局部变量的值需要程序员设定, AutoSize是TControl的Object instance data - FAutoSize, 所以它一定会被compiler初设为0(False).

    --------------------------------------------------------------------------------

    autosize是类里面的字段
    类实例在初始时,会自动fillchar。
    var 
      A: Boolean
    是栈变量,临时性,随机性是栈变是的特性。

    --------------------------------------------------------------------------------

    参考:

    ms-help://borland.bds4/bds4ref/html/Variables.htm
    http://docwiki.embarcadero.com/RADStudio/XE8/en/Variables

    --------------------------------------------------------------------------------

    另外,Delphi会优化布尔表达式:

    function TControl.CheckNewSize(var NewWidth, NewHeight: Integer): Boolean;
    var
      W, H, W2, H2: Integer;
    begin
      Result := False;
      W := NewWidth;  // cx
      H := NewHeight; // cy
      if DoCanResize(W, H) then // 类函数,给程序员控制的机会,然后再继续传递。程序员要求限制,才进行限制
      begin
        W2 := W;
        H2 := H;
        Result := not AutoSize   // 通常情况下下AutoSize=false,这样Result就已经等于true
        or (DoCanAutoSize(W2, H2) and (W2 = W) and (H2 = H))  // 问题: 编译器优化的话,还会执行这个函数吗?
        or DoCanResize(W2, H2); // 这个函数也是如此 
        if Result then
        begin
          NewWidth := W2;
          NewHeight := H2;
        end;
      end;
    end;

    我觉得上面的代码是故意为之:
     Result := not AutoSize   
        or (DoCanAutoSize(W2, H2) and (W2 = W) and (H2 = H))  
        or DoCanResize(W2, H2);
    也就是说,当autoSize=false的时候,就不用执行后面的函数了。只有autoSize=true的时候,才需要执行函数。而现实正是:只有控件运行自动扩展大小的时候,才需要执行后面的函数做进一步检查。

    结论:Delphi编译器会优化布尔表达式。1楼的代码之所以这样写,是故意为之,正好符合现实的需求。这样写虽然容易迷惑,但也可认为是VCL创作人员的高超的技巧性。

    参考:http://bbs.2ccc.com/topic.asp?topicid=496677

  • 相关阅读:
    IE页面后退刷新数据加载问题
    小经验: html中 js脚本运行顺序:思路整理
    jquery 之 $.end() 和 $.siblings()
    AI安防智能化发展至今还存在哪些问题?
    别 荣
    你是魔鬼 荣
    完全版权所有的DataGrid操作类及其用法 荣
    几篇关于.net1.1到.net2.0升级的文章(转载)Microsoft .NET Framework 1.1 和 2.0(测试版)兼容性 荣
    几篇关于.net1.1到.net2.0升级的文章(转载)在VS2005 正确地创建、部署和维护由1.1迁移到ASP.NET 2.0 应用程序注意事项 荣
    关于老实 荣
  • 原文地址:https://www.cnblogs.com/findumars/p/4779383.html
Copyright © 2020-2023  润新知