C++中函数指针的使用
来源:互联网 发布:局域网限速软件app 编辑:程序博客网 时间:2024/06/05 08:30
在C/C++中存在着函数指针,即指向函数的指针。我目前已知的两种使用方法是:
#include <string.h>#include <stdio.h>typedef int* PINNT;#define PP int*int funcA(int a,int b);int funcB(int* a,int *b);int main(int argc, char *argv[]){int (*func)(int,int);//func = &funcA; func = funcA;//两种赋值给函数指针的方法都可以printf("%d",func(1,10));//printf("%d",(*func)(1,10));//两种调用函数指针的方法都可以//两种赋值方法和两种调用方法可以任选一种组合}int funcA(int a,int b){return a + b;}int funcB(int* a,int *b){(*a) = (*a) + (*b);return 1;}
这里int (*func)(int,int)定义了一个函数指针变量,可以接受函数名或者函数名的指针(这里我还没有很清楚)。
然后我在CSDN上面找到了一个例子,据说是华为的面试题目,代码如下,这时程序正常输出值为110。
#include <stdio.h> int inc(int a) { return(++a); } int multi(int*a,int*b,int*c) { return(*c=*a**b); } typedef int(FUNC1)(int); typedef int(FUNC2)(int*,int*,int*); void show(FUNC2 fun,int arg1, int*arg2) { FUNC1 *p = &inc; int temp =p(arg1); fun(&temp,&arg1, arg2); printf( "%d\n ",*arg2); } main() { int a; show(multi,10,&a); getchar(); return 0; }
一开始我没有理解这个用法的时候,以为这里使用的方法与方法一相同,故直接写成
typedef int(FUNC1)(int); typedef int(FUNC2)(int*,int*,int*); void show(FUNC2 fun,int arg1, int*arg2) { FUNC1 = inc; int temp =p(arg1); fun(&temp,&arg1, arg2); printf( "%d\n ",*arg2); }
这时候,编译器给出的提示是:
1>d:\vctest\test4\test4\test4.c(17) : error C2513: 'int (int)' : no variable declared before '='
FUNC1 = inc这个语句的左边没有变量被申明,这个错误提示说明利一点:FUNC是一种类型而不是一个变量。做出如下更正:
FUNC1 *p = inc;
程序正确,更加证明了
typedef int(FUNC1)(int); typedef int(FUNC2)(int*,int*,int*);
语句定义了两个自己的数据类型FUNC1和FUNC2,这两个类型申明的变量用保存函数指针
也可以采用下面的方式进行定义:
typedef int (*FUNC1)(int); typedef int (FUNC2)(int*,int*,int*); void show(FUNC2 fun,int arg1, int*arg2) { FUNC1 p = inc; int temp = p(arg1); fun(&temp,&arg1, arg2); printf( "%d\n ",*arg2); }
以上例子,涉及到的概念有函数指针,函数类型和函数指针变量
函数指针变量有两种实现方法:
1. 先申明函数指针类型,在用函数指针类型申明函数指针变量
//typedef void FunType(int); -- FunType:同样是一个函数指针类型。
typedef void (*PF)(int,int);//使用函数指针类型申明的变量就是函数指针变量//PF a = &inc//PF a = inc
2. 直接定义一个函数指针变量
void (*fPV)(int,int);fvp = inc // fvp = &inc
函数类型与函数指针变量
如何定义函数类型呢
typedef void (FT)(int,int);
申明的函数类型如何使用呢?
首先可以用来申明函数:
FT x,y等价于:
void x(int,int);
void y(int,int);
请看如下的示例:
#include <stdio.h> typedef int (FT)(int*,int*);FT x,y;main() { int a = 1,b=2;x(&a,&b);(*y)(&a,&b);}int x(int* a,int*b){*a = *a + *b;printf("%d\n",*a);}int y(int*a,int* b){*a = *a * (*b); printf("%d\n",*a);}
上面的程序输出3和6
#include <stdio.h> typedef int (FT)(int*,int*);FT x,y,*z,u;main() { int a = 1,b=2;x(&a,&b);(*y)(&a,&b);z = x;//这里x是一个函数名,但是编译器会把函数名转换为函数指针变量,所以也可以显式写成z = &x;x(&a,&b);//u = x;//这里出错了这里u是一个函数名称,是一个指针常量,类似于数组名称,是不能够被赋值的}int x(int* a,int*b){*a = *a + *b;printf("%d\n",*a);}int y(int*a,int* b){*a = *a * (*b); printf("%d\n",*a);}
上面的例子中FT *Z其实是申明了一个指向函数的函数指针变量,类似于int(*z)(int*,int*)。
最后再来看下那个困扰了我很久的例子吧:
#include <stdio.h> int inc(int a) { return(++a); } int multi(int*a,int*b,int*c) { return(*c=*a**b); } typedef int (*FUNC1)(int); typedef int (FUNC2)(int*,int*,int*); typedef int (FUNC3)(int*,int*,int*); void show(FUNC2 *fun,int arg1, int*arg2) //void show(FUNC2 fun,int arg1, int*arg2)//这里做这样的声明也一样是对的 { FUNC1 p = &inc; FUNC2 *q = multi;int temp = (*p)(arg1); (*fun)(&temp,&arg1, arg2);//fun(&temp,&arg1, arg2); //这里的两种调用方法和上面的恶两种声明方法可以任意组合,,原因是函数名和函数指针常量的隐式转换printf( "%d\n ",*arg2); } main() { int a; show(multi,1,&a); getchar(); return 0; }
最让我疑惑的是
void show(FUNC2 *fun,int arg1, int*arg2)
//void show(FUNC2 fun,int arg1, int*arg2)//这里做这样的声明也一样是对的
这两种声明方式都是对的,采用void show(FUNC2 fun,int arg1, int*arg2)//这里做这样的声明也一样是对的这种方式声明, show(multi,1,&a);调用时正确的,
而前面的代码中,我也说过//u = x;//这里出错了这里u是一个函数名称,是一个指针常量,类似于数组名称,是不能够被赋值的, 这个问题困扰我很久啊,仔细考虑之后,我个人的观点是:void show(FUNC2 fun,int arg1, int*arg2)使用一个函数做为形式参数,调用时传递一个函数名字给他就可以,也可以传递一个函数常量指针。类比于使用指针常量或者数组作函数书参数的时候
#include <stdio.h> typedef void (FT)(int* const a);FT z;void main(){int a[]={1,2,3,4,5};int b = 5; int *c = &b;z(a);//但是如果在这里给a = c是不对的}void z(int* const a){printf("%d",*a);}
- c中函数指针的使用
- C 中typedef 函数指针的使用
- C函数指针的使用
- c 函数指针的使用
- C / C++ 类中使用函数指针的小例子
- C语言中函数指针的简单使用
- 指针, 指针的指针, 数组, 指针数组, 数组指针, 指针函数, 函数指针 C/C++中函数指针的含义
- 【C/C++】函数中指针的传递
- c语言中函数指针与指针函数的区别
- C/C++中函数指针的含义
- C/C++中函数指针的含义
- C/C++中函数指针的含义
- C/C++中函数指针的含义
- C/C++中函数指针的含义
- C/C++中函数指针的含义
- C/C++中函数指针的含义
- C/C++中函数指针的含义
- C/C++中函数指针的含义
- 0003-MFC--文本的查找和替换
- SAP BADI的实现2-实现BADI
- ZOJ3641 Information Sharing
- 推荐照度范围
- windows server 2003下连接数据库
- C++中函数指针的使用
- 【在线研讨】《敏捷开发用户故事分类与组织结构(三期-3)》
- CF 135 E.Parking Lot(线段树)
- 1.一对多双向关联(班级对学生):Object,hbm
- C# 文件下载四方法
- SAP BADI的实现3-使用BADI过滤器
- JAVA类,去除字符串中的空格、回车、换行符、制表符
- 开源的Web应用测试工具14
- Port Windows IPC apps to Linux, Part 2: Semaphores and events