HOME, PRODUCTS, ARTICLESCONTACT

C++BUILDER AND DELPHI FEEDBACK

CALLING CLASS METHODS OR STATIC FUNCTIONS — AN INTRODUCTION FOR APPLICATION DEVELOPERS

Delphi's class methods are equivalent to the static functions of C++. Because the design goals of static functions or class methods are largely restricted to component and class design, application developers are often less familiar with the reasons class methods may be used, or why we want (or need) to call them the way we do. It may seem odd for instance that a class design might implement a method having to pass the very class instance calling the method to the method itself — and we might not even gain much mileage from such oddities. But because the reasons we might do this lay outside the usual realm of application development, this article distills what application developers need to understand about static functions and class methods.

CLASS METHODS AND STATIC FUNCTIONS PASS NO IMPLICIT SELF OR THIS POINTER/REFERENCE

While the usual instance or generic methods of an object or class implicitly process members of a Self or this parameter, "class" methods and static functions are distinguished by their lack of an implicit Self or this parameter — they have no idea what Self or this would refer to. Because there is no implicit Self or this parameter, Delphi class methods and C++ static functions can be called without object instantiation — with empty handles, references or pointers of the class. Because class methods and static functions cannot implicitly process Self or this, an unassigned object handle, pointer, or reference imposes no problems to internal handling, as it would for generic methods.

Because we do not have to instantiate an object to call its class method or static function, we can do this:

DELPHI EXAMPLE

Copy code to clipboard (JavaScript/IE).

C++ EXAMPLE

Copy code to clipboard (JavaScript/IE).

This might not register on your Richter scale, but it does mean you might be able to save a small measure of processing time here or there, especially by avoiding instantiation of particularly gargantuan classes.

DECLARATIONS

  • Object Pascal procedures are equivalent to functions returning void in C++. Because of this disparity, Delphi class methods are declared with the reserved terms, class procedure or class function.
  • C++ static function declarations are marked by the reserved word, static.

PROCESSING AN INSTANCE OF THE CLASS

Because class methods and static functions lack implicit Self or this pointers, if we need to process instances of the class, the class method or static function must process a reference, pointer or handle argument indicating the instance to be processed. Thus a class, TMemberType, might declare a class method or static function taking a MemberInstanceArgument pointer:

DELPHI CLASS METHOD DECLARATION

Copy code to clipboard (JavaScript/IE).

ANSI C++ STATIC FUNCTION DECLARATION

Copy code to clipboard (JavaScript/IE).

PROCESSING CLASS MEMBERS WITH CLASS METHOD OR STATIC FUNCTION CALLS

To process any instance of TMemberType, we simply pass the required pointer to ClassMethod, with the resultant call taking the curious form,

DELPHI CLASS METHOD CALL

Copy code to clipboard (JavaScript/IE).

ANSI C++ STATIC FUNCTION CALL

Copy code to clipboard (JavaScript/IE).

The handle, reference, or pointer we call the method from merely identifies the method. As we can call the method without instantiation (with an empty handle, reference or pointer), we might instead do this, even as there would be no benefit from doing so, because we have instantiated objects (the very MemberInstance argument) from which we could (and should) instead call the method:

DELPHI CLASS METHOD CALL FROM AN UNINSTANTIATED OBJECT OR CLASS

Copy code to clipboard (JavaScript/IE).

ANSI C++ STATIC FUNCTION CALL FROM AN UNINSTANTIATED OBJECT OR CLASS

Copy code to clipboard (JavaScript/IE).

C++BUILDER™ TRANSLATIONS OF DELPHI™ CLASS METHODS

To support the underlying class identification system, IDE-generated C++ headers translating such Delphi™ class methods to C++Builder™ ask for an additional TMetaClass pointer:

EXAMPLE

Copy code to clipboard (JavaScript/IE).

SUMMARY

A usual intention of class methods or static functions is to give you the opportunity to save processing time by calling the method without instantiation. The greater the proportions of the class, the more processing time and local resources you can save by doing so — if you don't have an instantiated member of the class from which to do so.

© Copyright 1995-2007, by ADVANCE Information Systems, Inc. ALL RIGHTS RESERVED.Copyright 1995-2007, by ADVANCE Information Systems, Inc. ALL RIGHTS RESERVED.

Firefox™.Best viewed in Mozilla Firefox™.