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.
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.
typedef
or using
for ReadabilityWe 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
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
std::array
with Function PointersIn 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
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.
Answers to questions are automatically generated and may not have been reviewed.
Learn to create flexible and modular code with function pointers