c/c++ 函数指针的用法 |
您所在的位置:网站首页 › 函数指针的定义及用法c语言 › c/c++ 函数指针的用法 |
【目录】 基本定义 c 函数指针使用举例 c++ 函数指针使用举例 函数指针作为函数参数 函数指针作为函数返回值 函数指针数组 typedef 简化函数指针操作
c语言函数指针的定义形式:返回类型 (*函数指针名称)(参数类型,参数类型,参数类型,…); c++函数指针的定义形式:返回类型 (类名称::*函数成员名称)(参数类型,参数类型,参数类型,….); 以下代码编译环境:codeblocks with gcc in win 7 c语言函数指针使用举例: #include #include 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; }
c++函数指针使用举例: #include using namespace std; class test { public: test() { cout |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |