• 关于整型Integer、Int32、Int64、IntPtr、UINT、UInt32、Cardinal、UInt64、UIntPtr、NativeUInt、Pointer、Handle


    知识点1:UIntPtr = NativeUInt = Pointer = Handle 随程序的位数改变而改变。如下

    所以以后再用指针的时候要这样:UintPtr/NativeUInt(实例) = 栈中指针内存编号

     以下是代码研究:

    unit Unit5;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
    
    type
      TForm5 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
      /// <summary>
      /// 定义一个类,测试隐式转换
      /// </summary>
      TPerson = class
        function makeLove(const i: Integer): string; overload;
        //function makeLove(const i: Int32): string; overload; // Integer = int32 所以这个重载会报错
        function makeLove(const i: IntPtr): string; overload;  // IntPtr <> int32 因为 它与程序的位数有关,所以允许重载.
        function makeLove(const i: Int64): string; overload; // uint32、uint 无符号整型,由于integer会溢出,隐式转换到了这个重载上.
      end;
    
    var
      Form5: TForm5;
    
    implementation
    
    {$R *.dfm}
    
    
    (*===============以下有无符号整型============= *)
    
    //Integer = Int32; 有符号32位整型; 两种表示法
    function a1(const i: Integer): string;
    begin
      Exit(i.ToString);
    end;
    
    function a2(const i: Int32): string;
    begin
      Exit(i.ToString);
    end;
    
    //int64 只有一种表示法
    function a3(const i: Int64): string;
    begin
      Exit(i.ToString);
    end;
    
    // IntPtr = NativeInt 表示有符号整数,若程序是32位的则表示 integer , 若程序是64位的 则表示 int64
    function a4(const i: IntPtr): string;
    begin
      Exit(i.ToString);
    end;
    
    function a5(const i: NativeInt): string;
    begin
      Exit(i.ToString);
    end;
    
    (*===============以下是无符号整型============= *)
    
    //UINT = UInt32 = Cardinal; 无符号32位整型; 三种表示法
    function b1(const i: UINT): string;
    begin
      Exit(i.ToString);
    end;
    
    function b2(const i: UInt32): string;
    begin
      Exit(i.ToString);
    end;
    
    function b3(const i: Cardinal): string;
    begin
      Exit(i.ToString);
    end;
    
    function b4(const i: UInt64): string;
    begin
      Exit(i.ToString);
    end;
    
    // UIntPtr = NativeUInt 表示无符号整数,若程序是32位的则表示 UInt32 , 若程序是64位的 则表示 UInt64
    function b5(const i: UIntPtr): string;
    begin
      Exit(i.ToString);
    end;
    
    function b6(const i: NativeUInt): string;
    begin
      Exit(i.ToString);
    end;
    
    procedure TForm5.Button1Click(Sender: TObject);
    var
      a: Integer;
      b: Cardinal;
      c: UInt64;
      pp: TPerson;
    begin
      a := 10;
      b := 10;
      c := 10;
      pp := TPerson.Create;
      try
        pp.makeLove(a);
        pp.makeLove(b); //由于 integer会溢出, 所以这里 隐式 把 b 转换成了 int64, 然后调用了 int64的重载
        pp.makeLove(c); //这里隐式转换了 但是 这样不安全会发生溢出的现象, delphi 没有那么完美了!!
    
        //如果想操作指针的话,那么用 UIntPtr 与 NativeUInt 是最合适的,Handle Pointer 都与 等同于 UIntPtr NativeUInt 随程序的位数 改变而改变
        //UIntPtr(pp) = pp 的 栈中指针
        //NativeUInt(pp) = pp 的 栈中指针
    
    
        //由于大多数人,都是开发32位程序,而为了最大程度兼容 integer这个 类型,所以很多方法 都是用 int64做入参 ,比如: function abc(const a: int64);
        //那么这个方法 就是可以接收 integer int64 与 32位程序的指针(指针会被隐式转换为int64,不溢出因为int64足以包括它了),我师傅 就是这么干的.
      finally
        pp.Free;
      end;
    end;
    
    { TPerson }
    
    function TPerson.makeLove(const i: Integer): string;
    begin
      Exit(i.ToString);
    end;
    
    function TPerson.makeLove(const i: IntPtr): string;
    begin
      Exit(i.ToString);
    end;
    
    function TPerson.makeLove(const i: Int64): string;
    begin
     Exit(i.ToString);
    end;
    
    end.
  • 相关阅读:
    做一个项目什么最重要(代码部分最多占40%)
    二叉树遍历:已知前序和中序,求后序
    [每日一题] OCP1z0-047 :2013-08-24 FLASHBACK—TABLE/PRIMARY KEY(FOREIGN KEY?)......98
    商业价值:苹果iTV,再一次改变世界?
    Nginx负载均衡简易方法
    Qt之设置窗口背景
    python mongodb ubuntu
    生物-生理-衰老:百科
    C#基础:线程之异步回调(委托)
    C#:同步调用、异步调用、异步回调
  • 原文地址:https://www.cnblogs.com/del88/p/6714401.html
Copyright © 2020-2023  润新知