![]() |
ezEngine
Release 25.03
|
A generic delegate class which supports static functions and member functions. More...
#include <Delegate.h>
Additional Inherited Members | |
![]() | |
InstancePtr | m_Instance |
A generic delegate class which supports static functions and member functions.
A delegate is a function pointer that may be used to call both simple C functions, as well as member functions of some class, which requires a 'this' pointer to go along with. The delegate can capture both these use cases dynamically, so this distinction does not need to be made statically, but is the choice of the user who assigns some function to the delegate.
Delegates have a rather strange syntax:
This defines a type 'SomeCallback' that can call any function that returns void and takes two parameters, the first being ezUInt32, the second being a float parameter. Now you can use SomeCallback just like any other function pointer type.
Assigning a C function as the value to the delegate is straight-forward:
Assigning a member function to the delegate is a bit more complex:
Here you have to construct a delegate of the proper type and pass along both the member function pointer as well as the class instance to call the function on.
Using the delegate to call a function is straight forward again:
Just treat the delegate like a simple C function and call it. Internally it will dispatch the call to whatever function is bound to it, independent of whether it is a regular C function or a member function.
The check to 'IsValid' is only required when the delegate might have a nullptr bound (i.e. no function has been bound to it). When you know the delegate is always bound, this is not necessary.