Arrays of Function Pointers

Is it possible to have an array of function pointers?

Yes, it is indeed possible to have an array of function pointers in C++. This can be a powerful technique for implementing things like command tables, state machines, or strategy patterns. Let's explore how to create and use arrays of function pointers.

Basic Array of Function Pointers

Here's a simple example of creating and using an array of function pointers:

#include <iostream>

void Func1() { std::cout << "Function 1\n"; }
void Func2() { std::cout << "Function 2\n"; }
void Func3() { std::cout << "Function 3\n"; }

int main() {
  // Declare an array of function pointers
  void (*funcArray[3])() = {Func1, Func2, Func3};  

  // Call each function in the array
  for (int i = 0; i < 3; ++i) {
    funcArray[i]();
  }
}
Function 1
Function 2
Function 3

In this example, we create an array funcArray that can hold three function pointers, each pointing to a function that takes no arguments and returns void.

Using typedef or using for Readability

We can improve readability by using typedef or using:

#include <iostream>

typedef void (*FuncPtr)();
// Or, in C++11 and later:
// using FuncPtr = void (*)();

void Func1() { std::cout << "Function 1\n"; }
void Func2() { std::cout << "Function 2\n"; }
void Func3() { std::cout << "Function 3\n"; }

int main() {
  FuncPtr funcArray[] = {Func1, Func2, Func3};  

  for (const auto& func : funcArray) {
    func();
  }
}
Function 1
Function 2
Function 3

Array of Function Pointers with Parameters

We can also create arrays of function pointers that take parameters:

#include <iostream>

int Add(int a, int b) { return a + b; }
int Subtract(int a, int b) { return a - b; }
int Multiply(int a, int b) { return a * b; }

int main() {
  int (*operations[])(int, int) = {
    Add, Subtract, Multiply};  

  int a = 10, b = 5;
  for (const auto& op : operations) {
    std::cout << op(a, b) << ", ";
  }
}
15, 5, 50

Using std::array with Function Pointers

In modern C++, we can use std::array for a more type-safe approach:

#include <array>
#include <iostream>

void Func1() { std::cout << "Function 1\n"; }
void Func2() { std::cout << "Function 2\n"; }
void Func3() { std::cout << "Function 3\n"; }

int main() {
  std::array funcArray = {Func1, Func2, Func3};  

  for (const auto& func : funcArray) {
    func();
  }
}
Function 1
Function 2
Function 3

Practical Example: Command Pattern

Arrays of function pointers can be used to implement patterns like the Command pattern:

#include <array>
#include <iostream>
#include <string>

class Player {
 public:
  void MoveLeft() {
    std::cout << "Moving left\n";
  }
  void MoveRight() {
    std::cout << "Moving right\n";
  }
  void Jump() {
    std::cout << "Jumping\n";
  }
};

using Command = void (Player::*)();

int main() {
  std::array commands = {
    &Player::MoveLeft,
    &Player::MoveRight,
    &Player::Jump
  };

  Player player;
  std::string input;

  while (true) {
    std::cout << "Enter command (0-2)"
      " or 'q' to quit: ";

    std::getline(std::cin, input);

    if (input == "q") break;

    int index = std::stoi(input);
    if (index >= 0 && index < 3) {
      (player.*commands[index])();
    } else {
      std::cout << "Invalid command\n";
    }
  }
}
Enter command (0-2) or 'q' to quit: 0
Moving left
Enter command (0-2) or 'q' to quit: 1
Moving right
Enter command (0-2) or 'q' to quit: 2
Jumping
Enter command (0-2) or 'q' to quit: q

This example demonstrates a simple command system where the player's actions are stored in an array of member function pointers.

Arrays of function pointers provide a powerful way to organize and manage collections of related functions. They can be used to implement callback systems, state machines, and other patterns that involve selecting and executing functions based on runtime conditions.

Callbacks and Function Pointers

Learn to create flexible and modular code with function pointers

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Function Pointer Performance
How do function pointers affect performance compared to direct function calls?
Improving Function Pointer Syntax Readability
Is there a way to make function pointer syntax more readable without using std::function?
Function Pointers and Overloading
How do function pointers interact with function overloading?
Function Pointers and Templates
How do function pointers interact with function templates?
Function Pointers and Default Arguments
Can we use function pointers with functions that have default arguments?
std::function vs Raw Function Pointers
Are there any limitations or drawbacks to using std::function instead of raw function pointers?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant