Introduction
Pointers to functions in C++ allow you to store the address of a function in a pointer variable, enabling you to call the function indirectly through the pointer. This can be particularly useful for callback functions, implementing function tables, and passing functions as arguments to other functions.
Defining and Using Function Pointers
Basic Example
To define a pointer to a function, you need to specify the function’s return type and parameter types.
Syntax
returnType (*pointerName)(parameterTypes);
Example: Defining and Using a Function Pointer
#include <iostream>
using namespace std;
// A simple function
void greet() {
cout << "Hello, World!" << endl;
}
int main() {
// Define a pointer to the greet function
void (*greetPtr)() = greet;
// Call the function using the pointer
greetPtr();
return 0;
}
Output
Hello, World!
Explanation
void (*greetPtr)() = greet;
defines a pointer to thegreet
function.greetPtr();
calls thegreet
function through the pointer.
Passing Function Pointers to Functions
You can pass function pointers as arguments to other functions, allowing for dynamic function calls.
Example: Passing Function Pointers to Functions
#include <iostream>
using namespace std;
// A function that takes two integers and returns their sum
int add(int a, int b) {
return a + b;
}
// A function that takes two integers and returns their product
int multiply(int a, int b) {
return a * b;
}
// A function that takes a function pointer and two integers
void compute(int (*funcPtr)(int, int), int x, int y) {
cout << "Result: " << funcPtr(x, y) << endl;
}
int main() {
// Call compute with add function
compute(add, 5, 3);
// Call compute with multiply function
compute(multiply, 5, 3);
return 0;
}
Output
Result: 8
Result: 15
Explanation
compute(int (*funcPtr)(int, int), int x, int y)
takes a function pointer as an argument.compute(add, 5, 3);
calls thecompute
function with theadd
function pointer.compute(multiply, 5, 3);
calls thecompute
function with themultiply
function pointer.
Array of Function Pointers
You can create an array of function pointers to store multiple function addresses.
Example: Array of Function Pointers
#include <iostream>
using namespace std;
// Functions to be stored in the array
void add(int a, int b) {
cout << "Sum: " << a + b << endl;
}
void subtract(int a, int b) {
cout << "Difference: " << a - b << endl;
}
void multiply(int a, int b) {
cout << "Product: " << a * b << endl;
}
int main() {
// Define an array of function pointers
void (*funcPtr[])(int, int) = {add, subtract, multiply};
// Call each function using the array of function pointers
for (int i = 0; i < 3; i++) {
funcPtr[i](5, 3);
}
return 0;
}
Output
Sum: 8
Difference: 2
Product: 15
Explanation
void (*funcPtr[])(int, int) = {add, subtract, multiply};
defines an array of function pointers.- The loop iterates through the array and calls each function using the function pointers.
Using Function Pointers with Classes
Function pointers can also be used with member functions of classes. However, the syntax is slightly different.
Example: Function Pointers with Classes
#include <iostream>
using namespace std;
class Calculator {
public:
// Static member functions
static int add(int a, int b) {
return a + b;
}
static int subtract(int a, int b) {
return a - b;
}
};
int main() {
// Define a pointer to a member function
int (*funcPtr)(int, int);
// Assign the add function to the pointer
funcPtr = Calculator::add;
cout << "Sum: " << funcPtr(5, 3) << endl;
// Assign the subtract function to the pointer
funcPtr = Calculator::subtract;
cout << "Difference: " << funcPtr(5, 3) << endl;
return 0;
}
Output
Sum: 8
Difference: 2
Explanation
int (*funcPtr)(int, int);
defines a pointer to a member function.funcPtr = Calculator::add;
assigns theadd
function to the pointer.funcPtr = Calculator::subtract;
assigns thesubtract
function to the pointer.
Example Programs
Example 1: Menu-Driven Program
#include <iostream>
using namespace std;
// Function prototypes
void add(int a, int b) {
cout << "Sum: " << a + b << endl;
}
void subtract(int a, int b) {
cout << "Difference: " << a - b << endl;
}
void multiply(int a, int b) {
cout << "Product: " << a * b << endl;
}
void divide(int a, int b) {
if (b != 0) {
cout << "Quotient: " << a / b << endl;
} else {
cout << "Division by zero error!" << endl;
}
}
int main() {
// Define an array of function pointers
void (*operations[])(int, int) = {add, subtract, multiply, divide};
int choice, a, b;
while (true) {
cout << "Menu:\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 5) break;
if (choice >= 1 && choice <= 4) {
cout << "Enter two numbers: ";
cin >> a >> b;
operations[choice - 1](a, b); // Call the chosen operation
} else {
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
Output
Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 1
Enter two numbers: 5 3
Sum: 8
Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 4
Enter two numbers: 10 2
Quotient: 5
Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 5
Explanation
- The program defines an array of function pointers for different operations.
- The user selects an operation from the menu, and the corresponding function is called using the function pointer.
Example 2: Function Pointer as Callback
#include <iostream>
using namespace std;
// Function to be used as a callback
void onEvent() {
cout << "Event triggered!" << endl;
}
// Function that accepts a callback function pointer
void triggerEvent(void (*callback)()) {
// Simulate an event
cout << "Triggering event..." << endl;
callback(); // Call the callback function
}
int main() {
// Pass the onEvent function as a callback
triggerEvent(onEvent);
return 0;
}
Output
Triggering event...
Event triggered!
Explanation
- The
onEvent
function is defined to be used as a callback. - The
triggerEvent
function accepts a function pointer and calls the callback function. triggerEvent(onEvent);
passes theonEvent
function as a callback totriggerEvent
.
Conclusion
Pointers to functions in C++ provide a powerful way to store and manipulate function addresses, enabling dynamic function calls, callback mechanisms, and the implementation of function tables. This chapter covered the basics of defining and using function pointers, passing function pointers to functions, creating arrays of function pointers, and using function pointers with classes. Example programs demonstrated practical applications of function pointers, including a menu-driven program and a callback mechanism. Understanding how to use pointers to functions effectively is essential for writing flexible and reusable C++ programs.