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.

...全文
108 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分鼓励!
控件包包括: 1.TOBXPBarMenu ========== 就是大名鼎鼎的XPMenu控件名,这个控件包是Kingron修改过的,我对它也进行过一些小修改。 2.TOBCdUtils ==========  用来控制CD-ROM的开关,里面提供两个函数,直接调用就是了。 3.TOBCreateShortcut ==========  用于在桌面上创建快捷方式,如果你写安装程序的话,相信会节省不少代码。 4.TOBDirectories ========== 取得系统相关的各种路径,不用调用函数,直接读取控件相关属性就可以,十实用。 5.TOBGammaPanel ==========  十漂亮和专业的颜色取色器,不是取其它窗口的颜色,而且提供一个容器让用户选择颜色。 6.TOBFormMagnet ==========  不用写任何代码,让你的程序实现Winamp的磁性窗口效果。 7.TOBStarfield ==========  产生满天星空的效果,做关于窗口的话效果真会让人吃惊。 8.TOBThread ==========  多线程控件,再也不用为使用线程而新建一个文件了。而且提供多个函数和OnFinish线程完成、OnFinishAll所有线程完成等重要事件。 9.TOBZoom ==========  一个类似Windows放大镜的控件,如果需要写图像漫游的话就十合适了。 10.TOBZlibMultiple ==========  使用了Delphi的ZLib进行压缩,不同的是它可以将多个文件、整个目录压缩到一个文件中,还可以进行解压。十适合用在程序的备份功能上。 11.TOBSearchFile ==========  可以为你取得指定目录下面的所有文件名,相信很多人都需要这个控件。提供OnChangeDir事件让你在查找过程中进入子目录也能得到处理。 12.TOBDragDrop ==========  加入此控件后,你的程序就可以接受浏览器的文件拖入。 13.TOBAssociateExtension ==========  使你的程序有关联某种文件的能力。以前写此类相关代码真的好烦,现在好了,一个控件就搞定了。 14.TOBIQShine ==========  使得某个控件产生星光闪闪的效果,十的抢眼,好看。程序中将当前输入焦点设置为星光闪闪是一个不错的选择。 15.TOBWavePlayer ==========  顾名思义,就是一个播放Wav文件的控件,提供循环播放的功能。 16.TOBTimeLimit ==========  如果你要设置程序只能使用到某个日期的话,只需放上此控件,并设置日期就可以了。 17.TOBAutoSizeCompo ==========  使你的程序能根据窗口大小,自动放大缩小窗口控件的大小及位置。 18.TOBInstances ==========  使用此控件可以使你的程序只会运行一个实例,而你却无需编写任何代码。 19.TOBFileStore ==========  可以将任何文件保存到窗口上。以前要写这样的功能,文件只能保存到资源上,很不方便。 20.TOBPictureStore ==========  可以组织一些不可见的图片。作者一般使用在自画的控件上。 1.1版本新增 : 21.TOBSysInfo ==========  收集系统CPU、显卡、网卡、系统等信息,资料十齐全。感谢wind8bell提供资料。 22.TOBFormButton ==========  使得你的程序可以在标题栏添加按钮,并提供多种按钮图标或自定义图标。

5,391

社区成员

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

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