我最近在看李維的<Inside VCL>.李維在書中第一章中舉的第一個(gè)console程序中有以下的函數(shù)聲明:
function WindowPRoc(Window:HWND;AMessage:UINT;WParam:WPARAM;LParam:LPARAM):LRESULT;stdcall;export;
不知道你想過(guò)一個(gè)問(wèn)題沒(méi):在Delphi中是不區(qū)分大小寫的,也就是說(shuō)這里的聲明中變量名和類型名相同了.而我們?cè)趯W(xué)Pascal,C/C++的時(shí)候,老師常對(duì)我們說(shuō)不能去關(guān)鍵字作為變量名,為什么這里又能使用呢?
我剛開始看到這里的時(shí)候很困惑,所以我查看了Dephi7的<Delphi Language Guide>,終于找到了答案:那就是保留字(Directives)跟關(guān)鍵字(Reserved Words)的區(qū)別.
在<Delphi Language Guide>中提到:The following reserved words cannot be redefined or used as identifiers.也就是說(shuō)關(guān)鍵字是不能作為變量名的.書中對(duì)保留字的說(shuō)明如下:
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.也就是說(shuō)保留字則是可以用作變量名的.
這就解釋了為什么在前面的函數(shù)聲明中允許出現(xiàn)WParam:WPARAM;這種聲明.順便說(shuō)一下的是李維在<Inside VCL>一書中的第31頁(yè)小字解說(shuō)部分說(shuō)"C/C++ Builder為了更好的執(zhí)行效率而使用了關(guān)鍵字message來(lái)分派窗口消息...",這里關(guān)鍵字應(yīng)該改為保留字才對(duì),因?yàn)镸essage是保留字而非關(guān)鍵字.我們?cè)陂喿xVCL的源代碼時(shí)也常可以看到用Message作為變量名的.
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注