Originale-mail to me for new edition

 

Variable typecasts

 

You can cast any variable to any type, provided their sizes are the same and you do not mix integers with reals. (To convert numeric types, rely on standard functions like Int and Trunc.) Examples of variable typecasts include

Char(I)

Boolean(Count)

TSomeDefinedType(MyVariable)

Variable typecasts can appear on either side of an assignment statement. Thus

var MyChar: Char;

...

Shortint(MyChar) := 122;

assigns the character z (ASCII 122) to MyChar.

You can cast variables to a procedural type. For example, given the declarations

type Func = function(X: Integer): Integer;

var

  F: Func;

  P: Pointer;

  N: Integer;

you can make the following assignments.

F := Func(P);        { Assign procedural value in P to F }

Func(P) := F;        { Assign procedural value in F to P }

@F := P;             { Assign pointer value in P to F }

P := @F;             { Assign pointer value in F to P }

N := F(N);           { Call function via F }

N := Func(P)(N);     { Call function via P }

Variable typecasts can also be followed by qualifiers, as illustrated in the following example.

type

  TByteRec = record

    Lo, Hi: Byte;

  end;

  TWordRec = record

    Low, High: Word;

  end;

  PByte = ^Byte;

var

  B: Byte;

  W: Word;

  L: Longint;

  P: Pointer;

begin

  W := $1234;

  B := TByteRec(W).Lo;

  TByteRec(W).Hi := 0;

  L := $01234567;

  W := TWordRec(L).Low;

  B := TByteRec(TWordRec(L).Low).Hi;

  B := PByte(L)^;

end;

In this example, TByteRec is used to access the low- and high-order bytes of a word, and TWordRec to access the low- and high-order words of a long integer. You could call the predefined functions Lo and Hi for the same purpose, but a variable typecast has the advantage that it can be used on the left side of an assignment statement.

For information about typecasting pointers, see Pointers and pointer types. For information about casting class and interface types, see The as operator and Interface typecasts.

 

Topic groups

 

See also

Typecasts: Overview

Value typecasts

 

 

译文

 

变量类型转换

 

可以转换任何变量到任何类型,倘若它们具有相同的尺寸并且不要混淆整数和实数。(要转换数字类型,依靠诸如IntTrunc等函数即可。)下面是变量类型转换的范例:

 

Char(I)

Boolean(Count)

TSomeDefinedType(MyVariable)

 

变量类型转换可以出现在赋值语句的任何一边。如

 

var MyChar: Char;

...

Shortint(MyChar) := 122;

 

这里的语句将把字符zASCII 122)赋给MyChar

 

可以将变量转换为程序型类型。例如,对于给出的声明

 

type Func = function(X: Integer): Integer;

var

  F: Func;

  P: Pointer;

  N: Integer;

 

可以有如下的赋值语句:

 

F := Func(P);        { P中的程序型值赋给F }

Func(P) := F;        { F中的程序型值赋给P }

@F := P;             { P的指针赋给F }

P := @F;             { F的指针赋给P }

N := F(N);           { 通过F调用函数 }

N := Func(P)(N);     { 通过P调用函数 }

 

变量类型转换还可以跟随在限定词之后,例如:

 

type

  TByteRec = record

    Lo, Hi: Byte;

  end;

  TWordRec = record

    Low, High: Word;

  end;

  PByte = ^Byte;

var

  B: Byte;

  W: Word;

  L: Longint;

  P: Pointer;

begin

  W := $1234;

  B := TByteRec(W).Lo;

  TByteRec(W).Hi := 0;

  L := $01234567;

  W := TWordRec(L).Low;

  B := TByteRec(TWordRec(L).Low).Hi;

  B := PByte(L)^;

end;

 

在本例中,TByteRec用于访问字(word)类型的低位和高位字节,TWordRec用于访问长整数(long integer)的低位和高位字。可以通过调用预定义函数LoHi达到相同目的,但使用变量类型转换更具优势:它能被用于赋值语句的左边。

关于对指针进行类型转换的信息,见指针和指针类型;关于对于类和接口类型进行转换的信息,见as运算符接口类型转换

 

主题组

 

相关主题

类型转换:概述

值类型转换