What is the difference between
TFuncOfIntToString = reference to function(x: Integer): string;
and
TFuncOfIntToString = function(x: Integer): string of object;
I use the of object
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let us consider the following three type declarations:
These are all very similar to each other. In terms of calling instances of each of these three types, the calling code is identical. The differences arise in what can be assigned to variables of these types.
Procedural types
TProcedureis a procedural type. You can assign to a variable of typeTProceduresomething of this form:This is a non object-oriented procedure. You cannot assign an instance or class method to a
TProcedurevariable. However, you can assign a static class method to aTProcedurevariable.Method pointers
TMethodis a method pointer. This is indicated by the presence ofof object. When you have a variable of typeTMethodyou must assign either:So you can assign either of these:
The big difference between a procedural type and a method pointer is that the latter contains a reference to both code and data. A method pointer is often known as a two-pointer procedural type. A variable that contains a method pointer contains references to the code and the instance/class to call it on.
Consider the following code:
Now, although
method1andmethod2refer to the same piece of code, they are associated with different object instances. So, if we callWe are invoking
MyMethodon the two distinct instances. That code is equivalent to:Anonymous methods
Finally we come to anonymous methods. These are even more general purpose than procedural types and method pointers. You can assign any of the following to a variable defined using the
reference tosyntax:For example:
Anonymous methods, item 4 above, are those declared in-line in your code. For example:
The biggest benefit of anonymous methods when compared to the procedural types and method pointers is that they allow for variable capture. For example consider the following short program to illustrate:
This has the following output: