C++ Tutorial/Function/function pointers
Содержание
- 1 arguments passed by pointer
- 2 arguments passed by reference
- 3 array passed by pointer
- 4 Arrays of pointers to functions
- 5 Function pointer for overloaded function
- 6 orders two arguments using pointers
- 7 sorts an array using pointers
- 8 Use function pointers as a function parameter
- 9 Use typedef to define a function type for function pointer
- 10 Using function pointers
arguments passed by pointer
<source lang="cpp">#include <iostream>
using namespace std; int main(){ void centimize(double*); double var = 10.0; cout << "var = " << var << " inches" << endl; centimize(&var); cout << "var = " << var << " centimeters" << endl; return 0; } void centimize(double* ptrd){ *ptrd *= 2.54; }</source>
arguments passed by reference
<source lang="cpp">#include <iostream>
using namespace std; int main(){ void centimize(double&); double var = 10.0; cout << "var = " << var << " inches" << endl; centimize(var); cout << "var = " << var << " centimeters" << endl; return 0; } void centimize(double& v){ v *= 2.54; }</source>
array passed by pointer
<source lang="cpp">#include <iostream>
using namespace std; const int MAX = 5; int main(){ void centimize(double*); double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, 87.3 }; centimize(varray); for(int j=0; j<MAX; j++) cout << "varray[" << j << "]=" << varray[j] << " centimeters" << endl; return 0; } void centimize(double* ptrd){ for(int j=0; j<MAX; j++) *ptrd++ *= 2.54; }</source>
Arrays of pointers to functions
<source lang="cpp">#include <iostream>
void Square (int&,int&); void Cube (int&, int&); void Swap (int&, int &); void GetVals(int&, int&); void PrintVals(int, int); int main() { int valOne=1, valTwo=2; int i; const int MaxArray = 5; void (*pFuncArray[MaxArray])(int&, int&); pFuncArray[0] = GetVals; pFuncArray[1] = Square; pFuncArray[2] = Cube; pFuncArray[3] = Swap; pFuncArray[4] = Square; for (i=0;i<MaxArray; i++) { pFuncArray[i](valOne,valTwo); PrintVals(valOne,valTwo); } return 0; } void PrintVals(int x, int y) { std::cout << "x: " << x << " y: " << y << std::endl; } 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; }</source>
New value for ValOne: 1 New value for ValTwo: 2 x: 1 y: 2 x: 1 y: 4 x: 1 y: 64 x: 64 y: 1 x: 4096 y: 1
Function pointer for overloaded function
<source lang="cpp">#include <iostream> using namespace std; int myfunc(int a); int myfunc(int a, int b); int main() {
int (*fp)(int a); // pointer to int f(int) //int (*fp)(int a, int b); fp = myfunc; // points to myfunc(int) cout << fp(5); return 0;
} int myfunc(int a) {
return a;
} int myfunc(int a, int b) {
return a*b;
}</source>
5"
orders two arguments using pointers
<source lang="cpp">#include <iostream>
using namespace std; int main(){ void order(int*, int*); int n1=99, n2=11; int n3=22, n4=88; order(&n1, &n2); order(&n3, &n4); cout << "n1=" << n1 << endl; cout << "n2=" << n2 << endl; cout << "n3=" << n3 << endl; cout << "n4=" << n4 << endl; return 0; } void order(int* numb1, int* numb2){ if(*numb1 > *numb2){ int temp = *numb1; *numb1 = *numb2; *numb2 = temp; } }</source>
sorts an array using pointers
<source lang="cpp">#include <iostream>
using namespace std; int main(){ void bsort(int*, int); const int N = 10; int arr[N] = { 37, 84, 62, 91, 11, 65, 57, 28, 19, 49 }; bsort(arr, N); for(int j=0; j<N; j++) cout << arr[j] << " "; cout << endl; return 0; } void bsort(int* ptr, int n){ void order(int*, int*); int j, k; for(j=0; j<n-1; j++) for(k=j+1; k<n; k++) order(ptr+j, ptr+k); } void order(int* numb1, int* numb2){ if(*numb1 > *numb2){ int temp = *numb1; *numb1 = *numb2; *numb2 = temp; } }</source>
Use function pointers as a function parameter
<source lang="cpp">#include <iostream>
using namespace std; 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() { int valOne=1, valTwo=2; void (*pFunc)(int&, int&); pFunc = GetVals; PrintVals ( pFunc, valOne, valTwo); pFunc = Square; PrintVals ( pFunc, valOne, valTwo); pFunc = Cube; PrintVals ( pFunc, valOne, valTwo); pFunc = Swap; PrintVals ( pFunc, valOne, valTwo); return 0; } void PrintVals( void (*pFunc)(int&, int&),int& x, int& y) { cout << "x: " << x << " y: " << y << endl; pFunc(x,y); cout << "x: " << x << " y: " << y << endl; } 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) { cout << "New value for ValOne: "; cin >> rValOne; cout << "New value for ValTwo: "; cin >> rValTwo; }</source>
x: 1 y: 2 New value for ValOne: 1 New value for ValTwo: 2 x: 1 y: 2 x: 1 y: 2 x: 1 y: 4 x: 1 y: 4 x: 1 y: 64 x: 1 y: 64 x: 64 y: 1
Use typedef to define a function type for function pointer
<source lang="cpp">#include <iostream>
using namespace std; void Square (int&,int&); void Cube (int&, int&); void Swap (int&, int &); void GetVals(int&, int&); typedef void (*VPF) (int&, int&) ; void PrintVals(VPF,int&, int&); int main() { int valOne=1, valTwo=2; VPF pFunc; pFunc = GetVals; pFunc = Square; pFunc = Cube; pFunc = Swap; PrintVals ( pFunc, valOne, valTwo); return 0; } void PrintVals( VPF pFunc,int& x, int& y) { cout << "x: " << x << " y: " << y << endl; pFunc(x,y); cout << "x: " << x << " y: " << y << endl; } 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) { cout << "New value for ValOne: "; cin >> rValOne; cout << "New value for ValTwo: "; cin >> rValTwo; }</source>
x: 1 y: 2 x: 2 y: 1
Using function pointers
<source lang="cpp">#include <iostream>
void Square (int&,int&); void Cube (int&, int&); void Swap (int&, int &); void PrintVals(int, int); int main() { void (* pFunc) (int &, int &); int valOne=1, valTwo=2; PrintVals(valOne, valTwo); pFunc(valOne, valTwo); PrintVals(valOne, valTwo); pFunc = Square; PrintVals(valOne, valTwo); pFunc(valOne, valTwo); PrintVals(valOne, valTwo); pFunc = Cube; PrintVals(valOne, valTwo); pFunc(valOne, valTwo); PrintVals(valOne, valTwo); pFunc = Swap; PrintVals(valOne, valTwo); pFunc(valOne, valTwo); PrintVals(valOne, valTwo); return 0; } void PrintVals(int x, int y) { std::cout << "x: " << x << " y: " << y << std::endl; } 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; }</source>
x: 1 y: 2