Name
Object Keyword
Syntax
typeName=Subroutine headerof object; typeName= objectDeclarations... end; typeName= object(Base class)Declarations... end;
Description
The object
keyword has two distinct and unrelated
uses: to declare method types and to declare old-style classes.
Delphi Pascal lets you declare a procedure or function type as a plain subroutine or as a method. When you declare a plain procedural type, the type is a plain pointer type. You can assign pointers to subroutines whose signatures match the type, and the pointer value is an ordinary code pointer.
Using the
ofobjectsyntax, the procedural type becomes a method type. Methods have two parts: a code pointer and a data pointer. When you take a method’s address, you are capturing the code pointer for the method’s code (which is similar to a plain procedural pointer) and a data pointer (which is the object reference or class reference for a class method).Object-type declarations are obsolete and have been replaced by
classdeclarations. The old-styleobjectdeclarations exist primarily for backward compatibility with Turbo Pascal. New Delphi programs should useclassdeclarations instead.
Tips and Tricks
Properties of method type are called events.
The
TMethodtype is a record that holds a generic method pointer. You can castTMethodto a particular type, such asTNotifyEventto call the method.
Examples
type
TNotifyEvent = procedure(Sender: TObject) of object; TSimpleComponent = class(TComponent) private fOnChange: ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access