![](https://csdnimg.cn/release/download_crawler_static/547680/bg1.jpg)
Object Pascal
Language Guide
Inprise Corporation
100 Enterprise Way, Scotts Valley, CA 95066-3249
Borland
®
Delphi
™
5
for Windows 98, Windows 95, & Windows NT
![](https://csdnimg.cn/release/download_crawler_static/547680/bg2.jpg)
Refer to the file DEPLOY.TXT located in the root directory of your Delphi 5 product for a complete list of files that you
can distribute in accordance with the Delphi 5 License Statement and Limited Warranty.
Inprise may have patents and/or pending patent applications covering subject matter in this document. The
furnishing of this document does not give you any license to these patents.
C
OPYRIGHT © 1983, 1999 Inprise Corporation. All rights reserved. All Inprise and Borland brand and product names
are trademarks or registered trademarks of Inprise Corporation. Other brand and product names are trademarks or
registered trademarks of their respective holders.
Printed in the U.S.A.
HDE1350WW21002 3E2R899
9900010203-9 8 7654321
PDF
![](https://csdnimg.cn/release/download_crawler_static/547680/bg3.jpg)
i
Chapter 1
Introduction 1-1
What’s in this manual? . . . . . . . . . . . . . . 1-1
Delphi and Object Pascal . . . . . . . . . . . 1-1
Typographical conventions . . . . . . . . . . 1-2
Other sources of information . . . . . . . . . . . 1-2
Software registration and technical support . . 1-2
Part I
Basic language description
Chapter 2
Overview 2-1
Program organization . . . . . . . . . . . . . . . 2-1
Pascal source files . . . . . . . . . . . . . . . 2-1
Other files used to build applications . . . . 2-2
Compiler-generated files . . . . . . . . . . . 2-3
Example programs. . . . . . . . . . . . . . . . . 2-3
A simple console application . . . . . . . . . 2-3
A more complicated example . . . . . . . . . 2-4
A Windows application . . . . . . . . . . . . 2-5
Chapter 3
Programs and units 3-1
Program structure and syntax . . . . . . . . . . 3-1
The program heading . . . . . . . . . . . . . 3-2
The program uses clause . . . . . . . . . . . 3-2
The block . . . . . . . . . . . . . . . . . . . . 3-2
Unit structure and syntax . . . . . . . . . . . . . 3-3
The unit heading . . . . . . . . . . . . . . . . 3-3
The interface section . . . . . . . . . . . . . . 3-4
The implementation section. . . . . . . . . . 3-4
The initialization section. . . . . . . . . . . . 3-4
The finalization section . . . . . . . . . . . . 3-5
Unit references and the uses clause . . . . . . . 3-5
The syntax of a uses clause . . . . . . . . . . 3-5
Multiple and indirect unit references . . . . 3-6
Circular unit references . . . . . . . . . . . . 3-7
Chapter 4
Syntactic elements 4-1
Fundamental syntactic elements . . . . . . . . . 4-1
Special symbols. . . . . . . . . . . . . . . . . 4-2
Identifiers . . . . . . . . . . . . . . . . . . . . 4-2
Qualified identifiers . . . . . . . . . . . . 4-2
Reserved words . . . . . . . . . . . . . . . . . 4-3
Directives. . . . . . . . . . . . . . . . . . . . . 4-3
Numerals. . . . . . . . . . . . . . . . . . . . . 4-4
Labels. . . . . . . . . . . . . . . . . . . . . . . 4-4
Character strings . . . . . . . . . . . . . . . . 4-4
Comments and compiler directives. . . . . . . . 4-5
Expressions . . . . . . . . . . . . . . . . . . . . . 4-5
Operators. . . . . . . . . . . . . . . . . . . . . 4-6
Arithmetic operators . . . . . . . . . . . . 4-6
Boolean operators . . . . . . . . . . . . . . 4-7
Logical (bitwise) operators . . . . . . . . . 4-8
String operators . . . . . . . . . . . . . . . 4-9
Pointer operators. . . . . . . . . . . . . . . 4-9
Set operators . . . . . . . . . . . . . . . . 4-10
Relational operators . . . . . . . . . . . . 4-10
Class operators. . . . . . . . . . . . . . . 4-11
The @ operator . . . . . . . . . . . . . . . 4-11
Operator precedence rules . . . . . . . . 4-12
Function calls . . . . . . . . . . . . . . . . . 4-13
Set constructors . . . . . . . . . . . . . . . . 4-13
Indexes . . . . . . . . . . . . . . . . . . . . . 4-13
Typecasts . . . . . . . . . . . . . . . . . . . . 4-14
Value typecasts. . . . . . . . . . . . . . . 4-14
Variable typecasts . . . . . . . . . . . . . 4-14
Declarations and statements. . . . . . . . . . . 4-16
Declarations . . . . . . . . . . . . . . . . . . 4-16
Statements . . . . . . . . . . . . . . . . . . . 4-16
Simple statements . . . . . . . . . . . . . . . 4-16
Assignment statements . . . . . . . . . . 4-17
Procedure and function calls . . . . . . . 4-17
Goto statements . . . . . . . . . . . . . . 4-18
Structured statements. . . . . . . . . . . . . 4-19
Compound statements . . . . . . . . . . 4-19
With statements . . . . . . . . . . . . . . 4-20
If statements . . . . . . . . . . . . . . . . 4-21
Case statements . . . . . . . . . . . . . . 4-23
Control loops. . . . . . . . . . . . . . . . 4-24
Repeat statements . . . . . . . . . . . . . 4-24
While statements. . . . . . . . . . . . . . 4-25
For statements . . . . . . . . . . . . . . . 4-25
Blocks and scope . . . . . . . . . . . . . . . . . 4-27
Blocks. . . . . . . . . . . . . . . . . . . . . . 4-27
Scope . . . . . . . . . . . . . . . . . . . . . . 4-27
Naming conflicts. . . . . . . . . . . . . . 4-28
Contents
![](https://csdnimg.cn/release/download_crawler_static/547680/bg4.jpg)
ii
Chapter 5
Data types, variables, and constants 5-1
About types. . . . . . . . . . . . . . . . . . . . . 5-1
Simple types . . . . . . . . . . . . . . . . . . . . 5-2
Ordinal types . . . . . . . . . . . . . . . . . . 5-2
Integer types. . . . . . . . . . . . . . . . . 5-3
Character types . . . . . . . . . . . . . . . 5-4
Boolean types . . . . . . . . . . . . . . . . 5-5
Enumerated types . . . . . . . . . . . . . 5-6
Subrange types . . . . . . . . . . . . . . . 5-7
Real types . . . . . . . . . . . . . . . . . . . . 5-8
String types . . . . . . . . . . . . . . . . . . . . . 5-9
Short strings. . . . . . . . . . . . . . . . . . .5-11
Long strings. . . . . . . . . . . . . . . . . . . 5-11
WideString . . . . . . . . . . . . . . . . . . .5-12
About extended character sets . . . . . .5-12
Working with null-terminated strings . . . .5-12
Using pointers, arrays, and string
constants . . . . . . . . . . . . . . . . . .5-13
Mixing Pascal strings and
null-terminated strings . . . . . . . . . .5-14
Structured types . . . . . . . . . . . . . . . . . .5-15
Sets. . . . . . . . . . . . . . . . . . . . . . . .5-16
Arrays . . . . . . . . . . . . . . . . . . . . . .5-16
Static arrays . . . . . . . . . . . . . . . . .5-17
Dynamic arrays . . . . . . . . . . . . . . .5-17
Array types and assignments . . . . . . .5-20
Records . . . . . . . . . . . . . . . . . . . . .5-20
Variant parts in records . . . . . . . . . .5-21
File types . . . . . . . . . . . . . . . . . . . .5-23
Pointers and pointer types . . . . . . . . . . . .5-24
Overview of pointers . . . . . . . . . . . . .5-24
Pointer types . . . . . . . . . . . . . . . . . .5-26
Character pointers . . . . . . . . . . . . .5-26
Other standard pointer types . . . . . . .5-26
Procedural types . . . . . . . . . . . . . . . . . .5-27
Procedural types in statements and
expressions . . . . . . . . . . . . . . . . . .5-28
Variant types . . . . . . . . . . . . . . . . . . . .5-29
Variant type conversions . . . . . . . . . . .5-30
Variants in expressions . . . . . . . . . . . .5-32
Variant arrays . . . . . . . . . . . . . . . . . .5-32
OleVariant. . . . . . . . . . . . . . . . . . . .5-33
Type compatibility and identity . . . . . . . . .5-33
Type identity . . . . . . . . . . . . . . . . . .5-33
Type compatibility . . . . . . . . . . . . . . .5-34
Assignment-compatibility. . . . . . . . . . .5-34
Declaring types . . . . . . . . . . . . . . . . . . 5-35
Variables . . . . . . . . . . . . . . . . . . . . . . 5-36
Declaring variables . . . . . . . . . . . . . . 5-36
Absolute addresses . . . . . . . . . . . . 5-37
Dynamic variables. . . . . . . . . . . . . 5-37
Thread-local variables. . . . . . . . . . . 5-38
Declared constants . . . . . . . . . . . . . . . . 5-38
True constants . . . . . . . . . . . . . . . . . 5-38
Constant expressions . . . . . . . . . . . 5-39
Resource strings . . . . . . . . . . . . . . 5-40
Typed constants . . . . . . . . . . . . . . . . 5-40
Array constants . . . . . . . . . . . . . . 5-41
Record constants . . . . . . . . . . . . . . 5-41
Procedural constants . . . . . . . . . . . 5-42
Pointer constants. . . . . . . . . . . . . . 5-42
Chapter 6
Procedures and functions 6-1
Declaring procedures and functions . . . . . . . 6-1
Procedure declarations . . . . . . . . . . . . . 6-2
Function declarations . . . . . . . . . . . . . . 6-3
Calling conventions . . . . . . . . . . . . . . . 6-4
Forward and interface declarations . . . . . . 6-5
External declarations . . . . . . . . . . . . . . 6-6
Linking to .OBJ files . . . . . . . . . . . . . 6-6
Importing functions from DLLs . . . . . . 6-7
Overloading procedures and functions . . . . 6-7
Local declarations . . . . . . . . . . . . . . . . 6-8
Nested routines . . . . . . . . . . . . . . . 6-9
Parameters. . . . . . . . . . . . . . . . . . . . . . 6-9
Parameter semantics . . . . . . . . . . . . . 6-10
Value and variable parameters . . . . . . 6-10
Constant parameters . . . . . . . . . . . 6-11
Out parameters. . . . . . . . . . . . . . . 6-11
Untyped parameters. . . . . . . . . . . . 6-12
String parameters . . . . . . . . . . . . . . . 6-13
Array parameters . . . . . . . . . . . . . . . 6-13
Open array parameters . . . . . . . . . . 6-13
Variant open array parameters . . . . . . 6-15
Default parameters . . . . . . . . . . . . . . 6-16
Default parameters and overloaded
routines . . . . . . . . . . . . . . . . . . 6-17
Default parameters in forward and
interface declarations . . . . . . . . . . 6-17
Calling procedures and functions. . . . . . . . 6-17
Open array constructors . . . . . . . . . . . 6-18
![](https://csdnimg.cn/release/download_crawler_static/547680/bg5.jpg)
iii
Chapter 7
Classes and objects 7-1
Class types . . . . . . . . . . . . . . . . . . . . . 7-1
Inheritance and scope . . . . . . . . . . . . . 7-2
TObject and TClass . . . . . . . . . . . . . 7-3
Compatibility of class types . . . . . . . . 7-3
Object types . . . . . . . . . . . . . . . . . 7-3
Visibility of class members . . . . . . . . . . 7-4
Private, protected, and public
members . . . . . . . . . . . . . . . . . . 7-5
Published members. . . . . . . . . . . . . 7-5
Automated members . . . . . . . . . . . . 7-5
Forward declarations and mutually
dependent classes. . . . . . . . . . . . . . . 7-6
Fields . . . . . . . . . . . . . . . . . . . . . . . . 7-7
Methods. . . . . . . . . . . . . . . . . . . . . . . 7-8
Method implementations . . . . . . . . . . . 7-8
Inherited . . . . . . . . . . . . . . . . . . . 7-8
Self . . . . . . . . . . . . . . . . . . . . . . 7-9
Method binding . . . . . . . . . . . . . . . . 7-9
Static methods. . . . . . . . . . . . . . . . 7-9
Virtual and dynamic methods. . . . . . .7-10
Abstract methods . . . . . . . . . . . . . . 7-11
Overloading methods . . . . . . . . . . . . .7-12
Constructors . . . . . . . . . . . . . . . . . .7-12
Destructors . . . . . . . . . . . . . . . . . . .7-14
Message handlers . . . . . . . . . . . . . . .7-15
Implementing message handlers . . . . .7-15
Message dispatching . . . . . . . . . . . .7-15
Properties . . . . . . . . . . . . . . . . . . . . . .7-16
Property access . . . . . . . . . . . . . . . . .7-16
Array properties . . . . . . . . . . . . . . . .7-18
Index specifiers . . . . . . . . . . . . . . . . .7-19
Storage specifiers. . . . . . . . . . . . . . . .7-20
Property overrides and redeclarations. . . .7-20
Class references . . . . . . . . . . . . . . . . . .7-22
Class-reference types. . . . . . . . . . . . . .7-22
Constructors and class references. . . . .7-22
Class operators . . . . . . . . . . . . . . . . .7-23
The is operator . . . . . . . . . . . . . . .7-23
The as operator . . . . . . . . . . . . . . .7-24
Class methods . . . . . . . . . . . . . . . . .7-24
Exceptions . . . . . . . . . . . . . . . . . . . . .7-25
Declaring exception types . . . . . . . . . . .7-25
Raising and handling exceptions . . . . . . .7-25
Try...except statements . . . . . . . . . . .7-26
Re-raising exceptions . . . . . . . . . . . 7-28
Nested exceptions . . . . . . . . . . . . . 7-29
Try...finally statements . . . . . . . . . . 7-30
Standard exception classes and routines . . 7-30
Chapter 8
Standard routines and I/O 8-1
File input and output. . . . . . . . . . . . . . . . 8-1
Text files . . . . . . . . . . . . . . . . . . . . . 8-3
Untyped files. . . . . . . . . . . . . . . . . . . 8-4
Text-file device drivers . . . . . . . . . . . . . . . 8-4
Device functions. . . . . . . . . . . . . . . . . 8-5
The Open function. . . . . . . . . . . . . . 8-5
The InOut function . . . . . . . . . . . . . 8-5
The Flush function. . . . . . . . . . . . . . 8-6
The Close function. . . . . . . . . . . . . . 8-6
Handling null-terminated strings. . . . . . . . . 8-6
Wide-character strings . . . . . . . . . . . . . 8-7
Other standard routines . . . . . . . . . . . . . . 8-7
Part II
Special topics
Chapter 9
Dynamic-link libraries and packages 9-1
Calling DLLs . . . . . . . . . . . . . . . . . . . . 9-1
Static loading . . . . . . . . . . . . . . . . . 9-1
Dynamic loading. . . . . . . . . . . . . . . 9-2
Writing DLLs . . . . . . . . . . . . . . . . . . . . 9-3
The exports clause. . . . . . . . . . . . . . . . 9-4
Library initialization code . . . . . . . . . . . 9-4
Global variables in a DLL . . . . . . . . . . . 9-5
DLLs and System variables . . . . . . . . . . 9-5
Exceptions and runtime errors in DLLs. . . . 9-6
The shared-memory manager . . . . . . . . . 9-6
Packages . . . . . . . . . . . . . . . . . . . . . . . 9-7
Package declarations and source files. . . . . 9-7
Naming packages . . . . . . . . . . . . . . 9-8
The requires clause . . . . . . . . . . . . . 9-8
The contains clause . . . . . . . . . . . . . 9-8
Compiling packages . . . . . . . . . . . . . . 9-9
Generated files . . . . . . . . . . . . . . . . 9-9
Package-specific compiler directives . . . 9-9
Package-specific command-line
compiler switches . . . . . . . . . . . . 9-10