Function Pointers
Explore how function pointers work in D programming, including their syntax and use cases. Understand the difference between function pointers and delegates, and discover how they help store and execute functions later in your code. This lesson enhances your ability to manage behavior control and function calls effectively in your D applications.
We'll cover the following...
Function pointers are for storing addresses of functions to execute those functions at a later time. Function pointers in D are similar to their counterparts in the C programming language.
Delegates store both a function pointer and the context to execute that function pointer in. The stored context can either be the scope that the function execution will occur or a struct or class object.
Delegates also enable closures, a concept that is supported by most functional programming languages.
Function pointers
In the previous chapter, you saw that it is possible to take addresses of functions with the & operator. In one of those examples, we passed such an address to a function template.
Taking advantage of the fact that template type parameters can match any type, let’s pass a function pointer to a template to observe its type by ...