关于整型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.

 

转载于:https://www.cnblogs.com/del88/p/6714401.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值