Table of Contents

1. Pointers… to functions?!

In some designs it can be useful to set up a function to call at runtime, similar to how we might use polymorphism to figure out an object's type at runtime, or pointers-to-variables to point to different sets of data at runtime. We can do the same thing with functions.

Review 1. Why pointers?

  • ○ If you make your code harder to read, that means better job security.
  • ○ Sometimes you just gotta figure things out at runtime.
  • ○ Taking revenge on other developers by making everything pointers.

2. The old-timey way (C-style)

In the C-style way of declaring a function-to-be-pointed-to, you would create their declarations in the normal way.

TYPE FUNCNAME(PARAM1TYPE, PARAM2TYPE, ...);

Within main(), or wherever you're going to use this function pointer, we need to then declare a pointer - to a function. This should match the same form as the function declaration(s) you may be pointing to, except with * prefixing the pointer name and parentheses around it:

TYPE (*PTRNAME)(PARAM1TYPE, PARAM2TYPE, ...);

You can then use your PTRNAME as a variable, and point it to the address of some function…

PTRNAME = &FUNCTIONNAME;

And, you can call that function via the pointer, passing in arguments and storing its result in a variable:

VARIABLE = (*PTRNAME)( ARG1, ARG2 );

Review 2. Let's say we have these functions:

int Add( int a, int b )
{
    int result = a + b;
    cout << a << "+" << b << "=" << result << endl;
    return result;
}

int Sub( int a, int b )
{
    int result = a + b;
    cout << a << "-" << b << "=" << result << endl;
    return result;
}

Declaring the function pointer in the C-style way would look like:

  • ○ int (*funcPtr)();
  • ○ int (*funcPtr)(int, int);
  • ○ int *funcPtr(int, int);

Review 3. To point a C-style function pointer to a function, it would look like…

  • ○ funcPtr = Add;
  • ○ funcPtr = &Add;
  • ○ *funcPtr = &Add;
  • ○ *funcPtr = Add;

Review 4. To call a function via a C-style function pointer (and storing its result) would look like:

  • ○ int result = (&funcPtr)(a, b);
  • ○ int result = (*funcPtr)(a, b);
  • ○ int result = funcPtr(a, b);

2.1. Example: Math operations

So let's say we might do different math operations between two integers and want to return an integer result, we could define our two functions like this:

int Add( int a, int b )
{
    int result = a + b;
    cout << a << "+" << b << "=" << result << endl;
    return result;
}

int Sub( int a, int b )
{
    int result = a + b;
    cout << a << "-" << b << "=" << result << endl;
    return result;
}

Within main() is where we're going to decide which of these functions we want to call, and set up our function pointer. Let's say we have two integers a and b that we'll be using.

First, we declare our function pointer:

int (*funcPtr)(int, int);

We can then point this pointer to one function or the other. Note that both functions have the same parameter list and return type so this function pointer can point to either.

// Point to the Add function
funcPtr = &Add;
// or
// Point to the Sub function
funcPtr = &Sub;

To call the function, we invoke the function pointer. We will need to dereference the function pointer, like *funcPtr, but this will have to be inside of parentheses because of the order of operations for dereferencing vs. function calls.

int result = (*funcPtr)( a, b );

At this point one function or the other has been called indirectly via the function pointer. We coudl reassign the function pointer elseware during the program runtime as needed.

3. The more modern way (C++11-style)

Note that this way can only be used with versions of C++ since 2011. This may seem fine, but a lot of legacy software is written with the C++98 (1998) standard and a lot of modern features are not available. For a codebase using this old standard, you would have to use the C-style way.

First you'll need to make sure to include the functional library:

#include <functional>

Your functions will be declared/defined as normal. Within main() (or wherever you're going to use the function pointers), you can declare a function<> variable. Within the angle brackets < > is where you specify the function-to-be-pointed-to's return type and parameter list:

function<RETURN(PARAM1TYPE, PARAM2TYPE, ...)> PTRNAME;

You can then point this PTRNAME function pointer to any functions whose return type and parameter list matches what we've specified:

PTRNAME = FUNCNAME;

And we can call the function via the function pointer:

VARIABLE = PTRNAME( ARG1, ARG2 );

Review 5. Let's say we have these functions:

int Add( int a, int b )
{
    int result = a + b;
    cout << a << "+" << b << "=" << result << endl;
    return result;
}

int Sub( int a, int b )
{
    int result = a + b;
    cout << a << "-" << b << "=" << result << endl;
    return result;
}

Declaring the function pointer in the C++11-style way would look like:

  • ○ int (*funcPtr)(int, int);
  • ○ function<int(int, int)> funcPtr;
  • ○ int (*function)<int, int> funcPtr;

Review 6. To point a C++11-style function pointer to a function, it would look like…

  • ○ funcPtr = &Add;
  • ○ funcPtr = Add;
  • ○ *funcPtr = &Add;
  • ○ *funcPtr = Add;

Review 7. To call a function via a C++11-style function pointer (and storing its result) would look like:

  • ○ int result = (*funcPtr)( a, b );
  • ○ int result = funcPtr( a, b );
  • ○ int result = (&funcPtr)( a, b );

3.1. Example: Math operations

Once again let's have the two math operation functions to work with:

int Add( int a, int b )
{
    int result = a + b;
    cout << a << "+" << b << "=" << result << endl;
    return result;
}

int Sub( int a, int b )
{
    int result = a + b;
    cout << a << "-" << b << "=" << result << endl;
    return result;
}

Within main(), we will declare our function pointer variable, specifying the return type and parameter list:

function<int(int, int)> funcPtr;

We can now point this function pointer to one function or the other, as long as the return type and parameter list matches:

// Point to the Add function
funcPtr = Add;
// or
// Point to the Sub function
funcPtr = Sub;

We can then call the function via the function pointer and it looks like a normal function call:

int result = funcPtr( a, b );

Author: RachelWil

Created: 2026-10-14 Wed 11:12

Validate