【目錄】html
基本定義ios
c 函數指針使用舉例c++
函數指針做爲函數返回值this
函數指針數組spa
c語言函數指針的定義形式:返回類型 (*函數指針名稱)(參數類型,參數類型,參數類型,…);code
c++函數指針的定義形式:返回類型 (類名稱::*函數成員名稱)(參數類型,參數類型,參數類型,….);
htm
如下代碼編譯環境:codeblocks with gcc in win 7
#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; //第一種賦值方法 a = pfun1(); //第一種調用方法(推薦) printf("%d\n",a); b = (*pfun1)();//第二種調用方法 printf("%d\n",b); pfun2 = &fun2;//第二種賦值方法(推薦,由於和其餘數據指針賦值方法一致) pfun2(1,'a'); (*pfun2)(2,'b'); return 0; }
#include <iostream> using namespace std; class test { public: test() { cout<<"constructor"<<endl; } int fun1(int a, char c) { cout<<"this is fun1 call:"<<a<<" "<<c<<endl; return a; } void fun2(double d)const { cout<<"this is fun2 call:"<<d<<endl; } static double fun3(char buf[]) { cout<<"this is fun3 call:"<<buf<<endl; return 3.14; } }; int main() { // 類的靜態成員函數指針和c的指針的用法相同 double (*pstatic)(char buf[]) = NULL;//不須要加類名 pstatic = test::fun3; //能夠不加取地址符號 pstatic("myclaa"); pstatic = &test::fun3; (*pstatic)("xyz"); //普通成員函數 int (test::*pfun)(int, char) = NULL; //必定要加類名 pfun = &test::fun1; //必定要加取地址符號 test mytest; (mytest.*pfun)(1, 'a'); //調用是必定要加類的對象名和*符號 //const 函數(基本普通成員函數相同) void (test::*pconst)(double)const = NULL; //必定要加const pconst = &test::fun2; test mytest2; (mytest2.*pconst)(3.33); // //構造函數或者析構函數的指針,貌似不能夠,不知道c++標準有沒有規定不能有指向這二者的函數指針 // (test::*pcon)() = NULL; // pcon = &test.test; // test mytest3; // (mytest3.*pcon)(); return 0; }
#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++ 的形式差很少
// c 形式 #include <stdio.h> #include <stdlib.h> void fun(int k, char c) { printf("this is fun2 call:%d %c\n", k, c); } //fun1 函數的參數爲double,返回值爲函數指針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++ 形式 #include <iostream> using namespace std; class test { public: int fun(int a, char c) { cout<<"this is fun call:"<<a<<" "<<c<<endl; return a; } }; class test2 { public: // test2 的成員函數fun1,參數是double, //返回值是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; }
#include <stdio.h> #include <stdlib.h> float add(float a,float b){return a+b;} float minu(float a,float b){return a-b;} int main() { //定義一個函數指針數組,大小爲2 //裏面存放float (*)(float, float)類型的指針 float (*pfunArry[2])(float, float) = {&add, &minu}; double k = pfunArry[0](3.33,2.22);// 調用 printf("%f\n", k); k = pfunArry[1](3.33,2.22); printf("%f\n", k); return 0; } //c++ 可類比
#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-b); return a-b; } //用pfunType 來表示float(*)(float, float) typedef float(*pfunType)(float, float); int main() { pfunType p = &add;//定義函數指針變量 p(3.33, 2.22); pfunType parry[2] = {&add, &minu};//定義函數指針數組 parry[1](3.33, 2.22); //函數指針做爲參數能夠定義爲:void fun(pfunType p) //函數指針做爲返回值能夠定義爲:pfunType fun(); return 0; } //c++ 可類比
【版權聲明】轉載請註明出處 http://www.cnblogs.com/TenosDoIt/p/3164081.html