Delphi:所有常量都是常量,但有些常量比其他常量更多? [英] Delphi: All constants are constant, but some are more constant than others?
问题描述
请考虑:
const
clHotlight:TColor = $ 00FF9933;
clLink = clHotLight; // clias的别名
[错误] file.pas:期望的常量表达式
和替代的措辞:
const
clHotlight = TColor($ 00FF9933);
clLink = clHotLight; // clHotlight的别名
说明。
然后考虑:
const
AdministratorGUID:TGUID ='{ DE44EEA0-6712-11D4-ADD4-0006295717DA}';
SuperuserGUID = AdministratorGUID; //管理员GUID的别名
[错误] file.pas:期望的常量表达式
< <> 在声明之前添加了关键字 const
。
clHotlight:TColor = $ 00FF9933;
不是一个常数,而是一个类型化的常量(=静态变量),即编译器为内存中的一个TColor保留一个槽,初始值为 $ 00FF9933
因为该值以后可以更改(使用Assignable Const选项为ON),它不是一个实常数,不能被编译器在中接受clLink = clHotLight ;
clHotlight = TColor($ 00FF9933);
code> clHotlight = $ 00FF9933;
这是一个真正的常量,编译器将替换 clHotlight
值 $ 00FF9933
无论它出现在代码中。 clLink
以及。
阅读这个SO问题( 在Delphi 7中,为什么我可以为const赋值? / strong> )和所有好的答案...
EDIT:关于TGUID ... b $ b问题是,写 AdministratorGUID:TGUID ='{DE44EEA0-6712-11D4-ADD4-0006295717DA}';
不正确。
它使用一些编译器魔术来调用场景后面的 StringToGUID
,允许方便地将GUID表示为字符串,而不是自然的。他们是记录。
因此,尝试 AdministratorGUID ='{DE44EEA0-6712-11D4-ADD4-0006295717DA}';
。这不是GUID ...
解决方法是让类型常数和变量指向使用 绝对
指令的同一内存区域:
const
AdministratorGUID:TGUID ='{DE44EEA0-6712-11D4-ADD4-0006295717DA}';
var
SuperuserGUID:TGUID absolute AdministratorGUID; // Administrator的身份别名
RootGUID:TGUID absolute AdministratorGUID; // AdministratorGUID的别名
Consider:
const
clHotlight: TColor = $00FF9933;
clLink = clHotLight; //alias of clHotlight
[Error] file.pas: Constant expression expected
and the alternate wording that works:
const
clHotlight = TColor($00FF9933);
clLink = clHotLight; //alias of clHotlight
Explain.
Then consider:
const
AdministratorGUID: TGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}';
SuperuserGUID = AdministratorGUID; //alias of AdministratorGUID
[Error] file.pas: Constant expression expected
And fix.
Edit: Added keyword const
before declarations; someone didn't believe they were const.
clHotlight: TColor = $00FF9933;
is not a constant but a typed constant (=static variable), i.e. the compiler reserves a slot in memory for a TColor which will hold the value $00FF9933
initially at run time.
Because that value can be changed later (with the Assignable Const option ON), it is not a real constant and cannot be accepted by the compiler in clLink = clHotLight;
clHotlight = TColor($00FF9933);
is strictly the same as clHotlight = $00FF9933;
It is a true constant and the compiler will replace clHotlight
by its value $00FF9933
wherever it appears in the code. And for clLink
as well.
Read on this SO question (In Delphi 7, why can I assign a value to a const?) and all the good answers there...
EDIT: about TGUID...
The problem is that writing AdministratorGUID: TGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}';
is not proper.
It is using some compiler magic to call StringToGUID
behind the scene, allowing the convenience to express the GUID as a string which they are not by nature. They are records.
So, trying AdministratorGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}';
will not work. That is not a GUID...
A workaround is to have a typed constant and variables pointing to the same memory area using the absolute
directive:
const
AdministratorGUID: TGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}';
var
SuperuserGUID: TGUID absolute AdministratorGUID; //alias of AdministratorGUID
RootGUID: TGUID absolute AdministratorGUID; //alias of AdministratorGUID
这篇关于Delphi:所有常量都是常量,但有些常量比其他常量更多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!