Callbacks and Function Pointers

Arrays of Function Pointers

Is it possible to have an array of function pointers?

Abstract art representing computer programming

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.

This Question is from the Lesson:

Callbacks and Function Pointers

Learn to create flexible and modular code with function pointers

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Callbacks and Function Pointers

Learn to create flexible and modular code with function pointers

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 67 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved