Array Of Function Pointer C++

6 min read Jun 28, 2024
Array Of Function Pointer C++

Array of Function Pointers in C++

An array of function pointers in C++ is a powerful tool that allows you to store and execute different functions dynamically at runtime. This provides flexibility and efficiency in your code, enabling you to manage a collection of related functions in a structured way.

What is a Function Pointer?

A function pointer in C++ is a variable that holds the memory address of a function. It allows you to pass functions as arguments to other functions, store functions in data structures, and invoke them dynamically.

Declaring an Array of Function Pointers

To declare an array of function pointers, you need to specify the return type and the parameter types of the functions you want to store:

// Example: an array of function pointers that take an integer and return a double
double (*functionArray[5])(int); 

This declaration creates an array named functionArray with a size of 5 elements. Each element can hold the address of a function that takes an integer as an argument and returns a double.

Initializing the Array

You can initialize the array with the addresses of specific functions:

// Example: assigning function pointers to elements in the array
double square(int x) { return x * x; }
double cube(int x) { return x * x * x; }

double (*functionArray[5])(int) = { square, cube, /* ... */ };

This initializes the first two elements of the array with the addresses of the square and cube functions.

Using the Array to Execute Functions

To call a function through the array, you simply access the desired element and use the pointer to invoke the function:

int main() {
    int value = 3;
    double result = functionArray; // Call square function
    cout << "Square of " << value << " is: " << result << endl;

    result = functionArray; // Call cube function
    cout << "Cube of " << value << " is: " << result << endl;
}

This code calls the square and cube functions stored in the functionArray by accessing the elements at index 0 and 1, respectively.

Advantages of Using Array of Function Pointers

  • Dynamic Function Execution: Allows you to choose and execute specific functions based on runtime conditions.
  • Code Organization: Helps you group and manage related functions in a structured way.
  • Flexibility: Provides a way to pass function behavior as arguments to other functions.

Example Usage

Consider a scenario where you want to implement a calculator with different operations:

#include 

using namespace std;

// Function prototypes
double add(double x, double y);
double subtract(double x, double y);
double multiply(double x, double y);
double divide(double x, double y);

int main() {
    // Array of function pointers for calculator operations
    double (*operations[4])(double, double) = { add, subtract, multiply, divide };

    int choice;
    double num1, num2;

    cout << "Select operation:\n";
    cout << "1. Add\n";
    cout << "2. Subtract\n";
    cout << "3. Multiply\n";
    cout << "4. Divide\n";
    cin >> choice;

    cout << "Enter two numbers: ";
    cin >> num1 >> num2;

    // Execute the selected function
    double result = operations;

    cout << "Result: " << result << endl;

    return 0;
}

// Function definitions
double add(double x, double y) { return x + y; }
double subtract(double x, double y) { return x - y; }
double multiply(double x, double y) { return x * y; }
double divide(double x, double y) { return x / y; }

This example demonstrates how an array of function pointers can be used to dynamically choose and execute calculator operations based on user input.

Conclusion

Arrays of function pointers are a powerful tool in C++ that provide flexibility and efficiency in code by allowing you to manage and execute functions dynamically. They are particularly useful for implementing scenarios that require dynamic function selection and execution, such as calculators, event handlers, and custom function libraries.