Kingron,非常感谢你关于typecast的答复,特献上100分!
BCB 2001-08-05 04:43:59 It is sometimes useful to treat an expression as if it belonged to different type. A typecast allows you to do this by, in effect, temporarily changing an expression抯 type. For example, Integer('A') casts the character A as an integer.
The syntax for a typecast is
typeIdentifier(expression)
If the expression is a variable, the result is called a variable typecast; otherwise, the result is a value typecast. While their syntax is the same, different rules apply to the two kinds of typecast.
Value typecasts
Variable typecasts
---------------------
In a value typecast, the type identifier and the cast expression must both be ordinal types or both be pointer types. Examples of value typecasts include
Integer('A')
Char(48)
Boolean(0)
Color(2)
Longint(@Buffer)
The resulting value is obtained by converting the expression in parentheses. This may involve truncation or extension if the size of the specified type differs from that of the expression. The expression抯 sign is always preserved.
The statement
I := Integer('A');
assigns the value of Integer('A')梩hat is, 65梩o the variable I.
A value typecast cannot be followed by qualifiers and cannot appear on the left side of an assignment statement.
--------------------------------
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.