Name
Virtual Directive
Syntax
Method declaration; virtual;Description
You can declare a virtual method in a base class by using the
virtual directive or the
dynamic directive. The semantics of both
directives is the same. The only difference is the implementation of
the method and how it is called. For details, see Chapter 3.
Derived classes must use the override directive to
override the method.
Tips and Tricks
You should almost always use the
virtualdirective instead ofdynamic. In most cases,virtualmethods are faster and take up less memory thandynamicmethods.The
virtualdirective must follow thereintroduceandoverloaddirectives and precede the calling convention andabstractdirectives (if the declaration uses any of these directives).
Example
type WholeNumber = 0..MaxInt; TIterator = function(Item: Pointer): Boolean of object; // Abstract base class for a variety of collection classes. // Derived classes must override several methods to implement // the detailed behavior. TCollection = class protected constructor Create; virtual; function Add(Item: Pointer): WholeNumber; virtual; abstract; function GetCount: WholeNumber; virtual; abstract; function Delete(Index: WholeNumber): Pointer; virtual; abstract; public procedure Remove(Item: Pointer); virtual; procedure ForEach(Iterator: TIterator); virtual; end; // Abstract class for several different binary trees. // Implements the binary-tree methods, but leaves some of the // details to derived classes. TBinaryTree = class(TCollection) ...
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