Skip to main content
A function pointer is a variable that stores the memory address of executable code (a function) rather than a data value. It allows a program to invoke a function indirectly by dereferencing the pointer, provided the pointer’s type signature strictly matches the target function’s return type and parameter list.

Syntax and Declaration

The declaration of a function pointer requires specifying the exact signature of the functions it can point to. The asterisk * and the pointer name must be enclosed in parentheses to override standard operator precedence; otherwise, the compiler interprets it as a function returning a pointer.

Initialization and Assignment

A function pointer is initialized by assigning it the uninvoked name of a function. The address-of operator (&) is optional because a function name implicitly decays into a pointer to that function.

Invocation

Invoking a function through a pointer can be done using implicit or explicit dereferencing. Both approaches yield identical machine code.

Type Aliasing

Because raw function pointer syntax can become difficult to parse—especially when used as return types or parameters—C++ provides typedef and the modern using keyword to create type aliases.

Pointers to Member Functions

In C++, standard function pointers cannot store the address of non-static member functions. This is because non-static member functions require an implicit this pointer to operate on an object instance. To point to a non-static member function, you must use the pointer-to-member syntax (Class::*).
Invoking a pointer to a member function requires an instantiated object of the class and the use of the pointer-to-member operators (.* for objects/references, or ->* for pointers).
Note: Static member functions do not have an implicit this pointer. Therefore, they decay into standard function pointers and do not require the pointer-to-member syntax.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More