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.

...全文
131 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
BCB 2001-08-05
  • 打赏
  • 举报
回复
c++builder的引用没有任何限制:
(另一类型 &)变量=值;
say 2001-08-05
  • 打赏
  • 举报
回复
祈祷!
愿kingron大哥身心安康!
Kingron 2001-08-05
  • 打赏
  • 举报
回复
嗯,原来只使用过,没有注意到这个,知道了。
BCB 2001-08-05
  • 打赏
  • 举报
回复
!
BCB 2001-08-05
  • 打赏
  • 举报
回复
左值有限制,左右类型必须等长
Kingron 2001-08-05
  • 打赏
  • 举报
回复
是啊,现在在疗养,每天运动多一点儿了,原来整天在电脑前面~~~~~~~~~555555~~~~~~~5555,身体好差劲
Kingron 2001-08-05
  • 打赏
  • 举报
回复
赫赫,char(i):=5; ,这种用法我原来就用过了~~~~~~~~,不过一时没有写出来给大家共享,现在记性不好了,很多东西但是火花一闪,过后也就忘记了。 :(
BCB 2001-08-05
  • 打赏
  • 举报
回复
身体不好,要大补呀!!!!!
BCB 2001-08-05
  • 打赏
  • 举报
回复
这个邦助看了非常好!DELPHI的兄弟们应该看看!
第二个Variable typecasts 还可用于左值,相当于C++BUILDER的引用(t &)功能:
var i:integer;
char(i):=5; { 相当于bcb的 (char &)i=5 }


Kingron 2001-08-05
  • 打赏
  • 举报
回复
哎,不是戒网啊,是身体条件不好~~~~~~~:),咳~咳~~咳~~~。 :(

赫赫,找到了就好,在那个贴子里面,没有想到你的手那么快~~~~~~~~,我还来不及说呢!就变成[已解决]了。
BCB 2001-08-05
  • 打赏
  • 举报
回复
请勿戒网呀,呵呵,特献上100分鼓励!

5,933

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧