__closure
Go Up to Keywords, Alphabetical Listing Index
Category
Syntax
<type> ( __closure * <id> ) (<param list>);
Description
There are two kinds of function pointers that can be used in C++. The first is a simple pointer to a global function (not a member of a class). When dealing with class member functions, a simple pointer to a method is not sufficient to make a call. Instead, two pointers are required: one for the originating object and the second for the method address.
All VCL component events are declared as closures, in order to allow methods of a class to be called when the event is fired. The following code is an excerpt from the Classes.hpp header, showing the declaration of the TNotifyEvent, used in many events in the VCL (e.g. OnClick).
typedef void __fastcall (__closure *TNotifyEvent)(System::TObject* Sender);
The following example shows how a normal, global function pointer type is declared:
typedef void (*PointerToAFunction)();
It is used as follows:
/* Assigning a function to the function pointer */ PointerToAFunction a = SomeGlobalFunc;
To make this function pointer type into a method pointer, simply add __closure as follows:
typedef void (__closure *PointerToAFunction)();
The method pointer is used as follows:
/* Assigning a method to the method pointer */ PointerToAFunction a = &(someObject.SomeMemberFunc);