Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare type for a class method in Delphi?

How to declare type for a class procedure, for example

type
  TTest = class
    procedure Proc1;
    class procedure Proc2;
    class procedure Proc3; static;
  end;

  TProc1 = procedure of object;
  TProc2 = ???;
  TProc3 = ???;
like image 904
kludg Avatar asked Mar 24 '14 08:03

kludg


People also ask

How do you call a class in Delphi?

Class methods can also be called directly from an object (e.g., "myCheckbox. ClassName"). However object methods can only be called by an instance of a class (e.g., "myCheckbox. Repaint").

What are classes and objects in Delphi?

A class, or class type, defines a structure consisting of fields, methods, and properties. Instances of a class type are called objects. The fields, methods, and properties of a class are called its components or members. A field is essentially a variable that is part of an object.

What are methods in Delphi?

The Delphi method is a process used to arrive at a group opinion or decision by surveying a panel of experts. Experts respond to several rounds of questionnaires, and the responses are aggregated and shared with the group after each round.


1 Answers

TProc2 = procedure of object;

A class method still has a Self pointer. It's the class rather than the instance.

An interesting consequence of this is that it provides a way to implement event handlers without having to instantiate an object. For instance, you could use a class method of a class that that is never instantiated as a way to provide event handlers for the global Application object.

TProc3 = procedure;

A static class method has no Self pointer. It is assignment compatible with a plain procedural type.

Static class methods can be used as an alternative to globally scoped procedures. This does allow you to put such methods in a namespace, that of the class, and so avoid polluting the global namespace.

Take care when implementing static class methods that you do not call virtual class methods. Such calls are bound statically at compile time because the lack of a Self pointer means that dynamic polymorphic binding at runtime is not possible. Rather disappointingly the compiler fails to warn of this and so you do need to keep your wits about you.

like image 145
David Heffernan Avatar answered Oct 29 '22 16:10

David Heffernan