区分保留字跟关键字

类别:Delphi 点击:0 评论:0 推荐:

     我最近在看李维的<Inside VCL>.李维在书中第一章中举的第一个console程序中有以下的函数声明:

function WindowProc(Window:HWND;AMessage:UINT;WParam:WPARAM;LParam:LPARAM):LRESULT;stdcall;export;

不知道你想过一个问题没:在Delphi中是不区分大小写的,也就是说这里的声明中变量名和类型名相同了.而我们在学Pascal,C/C++的时候,老师常对我们说不能去关键字作为变量名,为什么这里又能使用呢?

     我刚开始看到这里的时候很困惑,所以我查看了Dephi7的<Delphi Language Guide>,终于找到了答案:那就是保留字(Directives)跟关键字(Reserved words)的区别.

    在<Delphi Language Guide>中提到:The following reserved words cannot be redefined or used as identifiers.也就是说关键字是不能作为变量名的.书中对保留字的说明如下:

Directives are words that are sensitive in specific locations within source code. Deriectives have special meanings in the Delphi language,but unlike reserved words,appear only in contexts where user-defined identifiers cannot occured.Heance-although it is inadvisable to do so-you can define an identifier  that looks exactly like a directive.也就是说保留字则是可以用作变量名的.

    这就解释了为什么在前面的函数声明中允许出现WParam:WPARAM;这种声明.顺便说一下的是李维在<Inside VCL>一书中的第31页小字解说部分说"C/C++ Builder为了更好的执行效率而使用了关键字message来分派窗口消息...",这里关键字应该改为保留字才对,因为Message是保留字而非关键字.我们在阅读VCL的源代码时也常可以看到用Message作为变量名的.

 

本文地址:http://com.8s8s.com/it/it5158.htm