A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. Constant expressions include numerals; character strings; true constants; values of enumerated types; the special constants True, False, and nil; and expressions built exclusively from these elements with operators, typecasts, and set constructors. Constant expressions cannot include variables, pointers, or function calls, except calls to the following predefined functions:
|
Abs |
High |
Low |
Pred |
Succ |
|
Chr |
Length |
Odd |
Round |
Swap |
|
Hi |
Lo |
Ord |
SizeOf |
Trunc |
This
definition of a constant expression is used in several places in
Object Pascal’s syntax specification. Constant expressions are required for
initializing global variables, defining subrange types, assigning ordinalities
to values in enumerated types, specifying default parameter values, writing case
statements, and declaring both true and typed constants.
Examples
of constant expressions:
100
'A'
256 -
1
(2.5 +
1) / (2.5 - 1)
'Borland'
+ ' ' + 'Developer'
Chr(32)
Ord('Z') - Ord('A') + 1
常量表达式就是表达式出现时,不用执行程序就能由编译器求值的表达式。常量表达式包括数字,字符串,真实常量,枚举类型的值,特别常量(True、False和nil),以及用运算符、类型转换、集合构造器等对上述这些表达式特别建立的表达式等。常量表达式不能包括变量、指针,也不能包括绝大多数函数调用。常量表达式中可以调用的函数仅限于如下预定义函数:
|
Abs |
High |
Low |
Pred |
Succ |
|
Chr |
Length |
Odd |
Round |
Swap |
|
Hi |
Lo |
Ord |
SizeOf |
Trunc |
在Object Pascal的语法说明中,常量表达式的定义用于这些场合:初始化全局变量,定义子界类型,向枚举类型赋以序数值,说明缺省参数的值,编写case语句,声明真实常量和类型常量。
下面是常量表达式的一些范例:
100
'A'
256 -
1
(2.5 +
1) / (2.5 - 1)
'Borland'
+ ' ' + 'Developer'
Chr(32)
Ord('Z') - Ord('A') + 1