3 ms·
Delphi captures the pointer-to-instance and pointer-to-function concept too. It uses them for event handlers: delegating behaviour to other classes. To do that,
by vintagedave 3y ago
Delphi captures the pointer-to-instance and pointer-to-function concept too. It uses them for event handlers: delegating behaviour to other classes. To do that, method pointers are often a 'fat pointer': the pair of object instance and function.
type TMethodPtr = procedure(x : Integer) of object; // 'of object' means an OO method pointer, not procedural
var p : TMethodPtr = foo.bar; // Captures both foo and bar
p(4); // Calls foo.bar(4)
Fun: you can use them in C++ too in C++Builder's dialect via the rather ugly syntax,
void(__closure * myClosure)(int); // myClosure can point to a method taking an int, returning void
myClosure = pObj->func; // Assigng myClosure; this captures both pObj and the address of func
myClosure(4); // Call it: this calls pObj->func(4)
Syntax aside, they're neat because they match type safety by the method signature not by the type of the object on which they're called (which lets yu use them to delegate to any classes, not just ones inheriting from specific ancestors.) This bypasses contravariance constraints too.
More info with a discussion on C++: https://www.codeproject.com/Articles/44874/C-Delegates-and-Borland-s-C-Builder-Event-Handling https://www.codeproject.com/Articles/44874/C-Delegates-and-B...