Pointers to Functions
Just as an array name is a constant pointer to the first element of the array, a function name is a constant pointer to the function. It is possible to declare a pointer variable that points to a function, and to invoke the function by using that pointer. It is very useful and it allows us to create programs that decide which functions to invoke based on user input. The only tricky part about function pointers is understanding the type of the object being pointed to. Just like a pointer to int points to an integer variable, a pointer to a function must point to a function of the appropriate return type and signature.
In the declaration
long (*funcPtr)(int);
The funcPtr variable is declared to be a pointer that points to a function that takes an integer parameter and returns a long. The funcPtr variable is declared to be a pointer that points to a function that takes an integer parameter and returns a long. The parentheses around *funcPtr are necessary because without the parenthesis, this would declare a function that takes an integer and returns a pointer to a long.
#include <iostream> void square(int&, int&); void cube(int&, int&); void swap(int&, int&); void getVals(int&, int&); void printVals(int, int); int main() { void (*pFunc)(int&, int&); bool fQuit = false; int valOne = 1, valTwo = 2; int choice; while (fQuit == false) { std::cout << "0 Quit 1 Change Values " << "2 Square 3 Cube 4 Swap: " << "\n"; std::cin >> choice; switch (choice) { case 1: pFunc = getVals; break; case 2: pFunc = square; break; case 3: pFunc = cube; break; case 4: pFunc = swap; break; default : fQuit = true; break; } if (fQuit) break; printVals(valOne, valTwo); pFunc(valOne, valTwo); printVals(valOne, valTwo); } return 0; } void printVals(int x, int y) { std::cout << "x: " << x << " y:" << y << "\n"; } void square(int &rX, int &rY) { rX *= rX; rY *= rY; } void cube(int &rX, int &rY) { int tmp; tmp = rX; rX *= rX; rX = rX * tmp; tmp = rY; rY *= rY; rY = rY * tmp; } void swap(int &rX, int &rY) { int temp; temp = rX; rX = rY; rY = temp; } void getVals(int &rValOne, int &rValTwo) { std::cout << "New value for valOne: "; std::cin >> rValOne; std::cout << "New value for valTwo: "; std::cin >> rValTwo; }
Output:
#include <iostream> void square(int&, int&); void cube(int&, int&); void swap(int&, int&); void getVals(int&, int&); void printVals(int, int); int main() { void (*pFunc)(int&, int&); bool fQuit = false; int valOne = 1, valTwo = 2; int choice; while (fQuit == false) { std::cout << "0 Quit 1 Change Values " << "2 Square 3 Cube 4 Swap: " << "\n"; std::cin >> choice; switch (choice) { case 1: pFunc = getVals; break; case 2: pFunc = square; break; case 3: pFunc = cube; break; case 4: pFunc = swap; break; default : fQuit = true; break; } if (fQuit) break; printVals(valOne, valTwo); pFunc(valOne, valTwo); printVals(valOne, valTwo); } return 0; } void printVals(int x, int y) { std::cout << "x: " << x << " y:" << y << "\n"; } void square(int &rX, int &rY) { rX *= rX; rY *= rY; } void cube(int &rX, int &rY) { int tmp; tmp = rX; rX *= rX; rX = rX * tmp; tmp = rY; rY *= rY; rY = rY * tmp; } void swap(int &rX, int &rY) { int temp; temp = rX; rX = rY; rY = temp; } void getVals(int &rValOne, int &rValTwo) { std::cout << "New value for valOne: "; std::cin >> rValOne; std::cout << "New value for valTwo: "; std::cin >> rValTwo; }
Output:
0 Quit 1 Change Values 2 Square 3 Cube 4 Swap: 1 x: 1 y:2 New value for valOne: 2 5 New value for valTwo: x: 2 y:5 0 Quit 1 Change Values 2 Square 3 Cube 4 Swap: 2 x: 2 y:5 x: 4 y:25 0 Quit 1 Change Values 2 Square 3 Cube 4 Swap: 3 x: 4 y:25 x: 64 y:15625 0 Quit 1 Change Values 2 Square 3 Cube 4 Swap: 4 x: 64 y:15625 x: 15625 y:64 0 Quit 1 Change Values 2 Square 3 Cube 4 Swap:
Passing Pointers to Functions to Other Functions:
The pointers to functions can be passed to other functions that may take action and then call the right function using the pointer.
The Output of program will will be same as program one. Both code has same working. In this code we passed pointer to function as argument to another function.
To create a pointer to member function, use the same syntax as with a pointer to a function, but include the class name and the scoping operator (::). Therefore, if pFunc points to a member function of the class Shape, which takes two integers and returns void, the declaration for pFunc is the following:
The pointers to functions can be passed to other functions that may take action and then call the right function using the pointer.
#include <iostream> void square(int&, int&); void cube(int&, int&); void swap(int&, int&); void getVals(int&, int&); void printVals(void (*)(int&, int&),int&, int&); int main() { void (*pFunc)(int&, int&); bool fQuit = false; int valOne = 1, valTwo = 2; int choice; while (fQuit == false) { std::cout << "0 Quit 1 Change Values " << "2 Square 3 Cube 4 Swap: " << "\n"; std::cin >> choice; switch (choice) { case 1: pFunc = getVals; break; case 2: pFunc = square; break; case 3: pFunc = cube; break; case 4: pFunc = swap; break; default : fQuit = true; break; } if (fQuit) break; printVals(pFunc, valOne, valTwo); } return 0; } void printVals(void (*pFunc)(int&, int&),int& x, int& y) { std::cout << "x: " << x << " y:" << y << "\n"; pFunc(x, y); std::cout << "x: " << x << " y:" << y << "\n"; } void square(int &rX, int &rY) { rX *= rX; rY *= rY; } void cube(int &rX, int &rY) { int tmp; tmp = rX; rX *= rX; rX = rX * tmp; tmp = rY; rY *= rY; rY = rY * tmp; } void swap(int &rX, int &rY) { int temp; temp = rX; rX = rY; rY = temp; } void getVals(int &rValOne, int &rValTwo) { std::cout << "New value for valOne: "; std::cin >> rValOne; std::cout << "New value for valTwo: "; std::cin >> rValTwo; }
The Output of program will will be same as program one. Both code has same working. In this code we passed pointer to function as argument to another function.
To create a pointer to member function, use the same syntax as with a pointer to a function, but include the class name and the scoping operator (::). Therefore, if pFunc points to a member function of the class Shape, which takes two integers and returns void, the declaration for pFunc is the following:
void (Shape::*pFunc)(int, int);
No comments:
Post a Comment