Pointer and Functions

In the lambdas module we saw the benefit of being able to pass code to another function. Similarly, you will sometimes want to pass a function to another function.

Table of Contents

  1. Pointers and Functions
  2. Pointers and Function Arguments
  3. Plain Old Function Pointers
  4. std::function vs Function Pointers
  5. Function Pointers as Function Arguments
  6. Creating Aliases for std::function and Function Pointers

Pointers and Functions

Pointers can be passed to function as arguments.

In Unreal Engine C++ code, for example, the following is common:

  • Passing around game objects using function parameters that are pointers.
  • Built-in functions that return pointers to objects managed by the engine.
  • Binding callback functions to events or timers using function pointers.

We’ll look at function pointers next.

Pointers and Function Arguments

At the end of the Lambdas module we looked using the std::function type from the <functional> header to pass lambdas as arguments to other functions.

Any function that accepts a std::function can also accept a reference to another function. Take a moment to review the example from the lambdas module.

Plain Old Function Pointers

The std::function type has been around since C++11, but before then we could accomplish the same thing using function pointers. The following are some example std::function definitions with the function pointer equivalents:

    // Standard function for a void function that takes no arguments.
    std::function<void(void)> myFunction;
    // Pointer to a void function that takes no arguments.
    void (*myFunction)(void);

    // Standard function that returns a bool and takes a single integer argument:
    std::function<bool(int)> myFunction;
    // Pointer to a function that returns a bool and take single integer argument:
    bool (*myFunction)(int);

    // Standard function that returns a double and take 3 args (string, int bool).
    std::function<double(std::string, int, bool)> myFunction;
    // Pointer to a function that returns a double and takes 3 args (string, int bool).
    double (*myFunction)(std::string, int, bool);

std::function vs Function Pointers

Here’s an example of using std::functions compared with function pointers in the context of variables:

Function Pointers as Function Arguments

Here’s a rewritten version of the the example from the lambdas module where a function is being passed to another function as an argument. In this rewrite we’re using a function pointer rather than std::function.

Creating Aliases for std::function and Function Pointers

As a human it can be hard to parse complex std::function or function pointer types. For that reason it’s nice to use the using keyword to create aliases for these complex types.

    using myStandardFunction = std::function<bool(int, double)>;
    using myFunctionPointer bool (*)(int, double);

    myStandardFunction myFunction1;
    myFunctionPointer myFunction2;

Here’s the filterPrint function from the above embedded example rewritten with an alias:

using predicateFunction = bool(*)(int);

void filterPrint(const std::vector<int>& vector, predicateFunction predicate) {
  for(auto element : vector) {
    if (predicate(element)) {
      std::cout << element << "\n";
    }
  }
};