C++: Function Pointers
Function pointers are like ordinary pointers but instead of pointing to variables, they point to functions.
The syntax for creating function pointers is a bit strange, and takes a bit of getting used to, but eventually you will get used to it.
Here is an example of declaring a function pointer:
void(*PointerName) ();
The pointer above (PointerName) can hold functions which have no parameters and are void's. This is why we have the "void" keyword, and have empty brackets afterwards..
The following is an example of how function pointers could be used:
#include <iostream>
using namespace std;
void TestFunction(){
cout << "Test...!" << endl;
}
void TestFunction2(){
cout << "Test2...!" << endl;
}
int main(){
void(*PointerName)();
PointerName = TestFunction;
PointerName();
PointerName = TestFunction2;
PointerName();
system("PAUSE");
return 0;
}
We can change the function to anything we like which in our example is a void, and has no parameters.
If we decided to point it to something which was an int for example, or something that accepted parameters we would get an error.
Back to C++

