Directory
Basic definition
Example of using C function pointers
Example of using C + + function pointers
function pointers as function arguments
function pointer as function return value
Array of function pointers
typedef simplifies function pointer manipulation
C language Function pointer definition form: return type (* function pointer name ) (parameter type, parameter type, parameter type, ...);
C + + function pointer definition form : return type (class name ::* function member name) (parameter type, parameter type, parameter type,...);
The following code compiles the environment: Codeblocks with GCC in Win 7
C language Function pointers use examples:
#include <stdio.h> #include <stdlib.h>int fun1 () { printf ("This is fun1 call\n"); return 1;} void fun2 (int k, char c) { printf ("This is fun2 call:%d%c\n", K, c);} int main () { int (*pfun1) () = NULL; void (*pfun2) (int, char) = NULL; int a, B; PFUN1 = fun1; The first method of assignment a = Pfun1 (); The first method of invocation (recommended) printf ("%d\n", a); b = (*pfun1) ();//The second method of invocation printf ("%d\n", b); Pfun2 = &fun2;//The second method of assignment (recommended because it is consistent with other data pointer assignment methods) pfun2 (1, ' a '); (*PFUN2) (2, ' B '); return 0;}
Examples of using C + + function pointers:
#include <iostream>using namespace Std;class test{public:test () {cout<< "constructor" <<end L } int fun1 (int a, char c) {cout<< "This was fun1 call:" <<a<< "" <<c<<endl; return A; } void Fun2 (double d) const {cout<< "This was fun2 call:" <<d<<endl; } Static double Fun3 (char buf[]) {cout<< "This was Fun3 call:" <<buf<<endl; return 3.14; }};int Main () {//class static member function pointer and C pointer use the same double (*pstatic) (char buf[]) = null;//do not need to add class name pstatic = TEST::FUN3;//Can not add Take the address symbol pstatic ("Myclaa"); Pstatic = &test::fun3; (*pstatic) ("XYZ"); Ordinary member function int (test::* pfun) (int, char) = NULL; Be sure to add class name Pfun = &test::fun1; Be sure to add the address symbol test mytest; (Mytest.*pfun) (1, ' a '); The call is certain to add class to the object name and the * symbol//const function (the basic ordinary member function is the same) void (test::* Pconst) (double) const = NULL; Be sure to add a const PCONST = &test::fun2; Test Mytest2; (mytest2.*pcOnst) (3.33);/////constructor or destructor pointers, seemingly not, do not know whether the C + + standard does not have a function pointer pointing to both//(test::* pcon) () = null;//Pcon = &test.test ;//Test mytest3;//(Mytest3.*pcon) (); return 0;}
function pointers as function parameters:
#include <stdio.h> #include <stdlib.h>void fun (int k, char c) { printf ("This is fun2 call:%d%c\n", K, c);} void Fun1 (void (*pfun) (int, char), int A, char c) { pfun (A, c);} int main () { fun1 (fun, 1, ' a '); return 0;} C + + has the same form
function pointers as function return values:
C form # include <stdio.h> #include <stdlib.h>void fun (int k, char c) { printf ("This is fun2 call:%d%c\n", K , c);} The argument for the FUN1 function is double, and the return value is the function pointer void (*) (int, char) void (*FUN1 (double D)) (int, char) { printf ("%f\n", d); return fun;} int main () { void (*p) (int, char) = FUN1 (3.33); P (1, ' a '); return 0;}
C + + form # include <iostream>using namespace Std;class test{public: int Fun (int A, char c) { cout< < "This was fun call:" <<a<< "" <<c<<endl; return A; }; Class test2{public : //test2 member function fun1, parameter is double, //return value is the member function pointer of test int (test::*) (int, char) Int ( Test::* fun1 (Double D)) (int, char) { cout<<d<<endl; Return &test::fun; }}; int main () { test mytest; Test2 Mytest2; Int (test::* p) (int, char) = MYTEST2.FUN1 (3.33); (mytest.*p) (1, ' a '); return 0;}
Array of function pointers:
#include <stdio.h> #include <stdlib.h>float add (float a,float b) {return a+b;} float Minu (float a,float b) {return a-A;} int main () { ///Defines a function pointer array, size 2 //Inside the float (*) (float, float) type of pointer float (*pfunarry[2]) (float, float) = { &add, &minu}; Double k = pfunarry[0] (3.33,2.22);//Call printf ("%f\n", k); K = pfunarry[1] (3.33,2.22); printf ("%f\n", k); return 0;} C + + can be analogous
typedef simplifies function pointer type:
#include <stdio.h> #include <stdlib.h>float add (float a,float b) { printf ("%f\n", a+b); return a+b;} float Minu (float a,float b) { printf ("%f\n", A-a); return a-B;} Float (*) (float, float) typedef float (*pfuntype) (float, float) with pfuntype, int main () { Pfuntype p = &add;// Define function pointer variable p (3.33, 2.22); Pfuntype Parry[2] = {&add, &minu};//defines an array of function pointers parry[1] (3.33, 2.22); A function pointer can be defined as a parameter: void Fun (Pfuntype p) //function pointer as a return value can be defined as: Pfuntype fun (); return 0;} C + + can be analogous
"Copyright Notice" Reproduced please specify the source http://www.cnblogs.com/TenosDoIt/p/3164081.html
Category: Basic C + +
The use of C + + function pointers