功能指针 [英] Function Pointer

查看:60
本文介绍了功能指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


函数指针的优点是什么?

Hi,
What is the Advantage of Function Pointer?

推荐答案

我不会用
来标记函数指针的使用. 优化或快速致电.取而代之的是我加深我的理解
指针变量方面的函数指针的集合.在C函数中,
就其本身而言,不是变量.函数的指针是变量和
可以用于赋值,数组,传递给函数等.

例子:

I wouldn''t label the use of function pointers in terms of
optimization or fast calls. Instead I style my understanding
of function pointers in the terms of pointer variables. In C a function,
by itself, is not a variable. Pointers to functions are variables and
can be used in assignments, arrays, passed to functions, etc.

Examples:

/* type definition of a function pointer */
typedef int (*FuncPointer)(void);

/* A function definition */
int foo(void)
{
return 6;
};

int bar(void)
{
return 10;
};

You cannot do this:
foo = bar; /* ERROR */

But you can do this:
FuncPointer p1,p2;
/* assignments */
p1 = foo;
p2 = bar;
p1 = p2;

/* array of function pointers */
FuncPointer parray[] = {foo,bar};
int d = parray[0]();

/* As a function parameter */
int Afunction(int i, FuncPointer p){return i*p();};
and the call:
int d = Afunction(2,foo);

/************ Code Example *********************/
#include <stdio.h>

typedef int (*FuncPointer)(void);

/* A function definition */
int foo(void)
{
return 6;
};

int bar(void)
{
return 10;
};

int Afunction(int i, FuncPointer p){return i*p();};

int main(void)
{
FuncPointer p1,p2;
FuncPointer parray[] = {foo,bar};
int d;

/* assignments */
p1 = foo;
p2 = bar;
printf("p2 returns %d\n",p2());

/* array of function pointers */
d = parray[0]();
printf("d = %d\n",d);

/* As a function parameter */
d = Afunction(2,foo);
printf("d = %d\n",d);
return 0;
}
/***********************END***************/



希望您能正确理解这一点



hope you can understand this properly


又一个答案

C语言中函数指针的一大用途是调用在运行时定义的函数.例如,C运行时库有两个例程qsort和bsearch,它们使用指向函数的指针,该函数调用该函数以比较要排序的两个项目.这使您可以根据希望使用的任何标准分别对任何内容进行排序或搜索.
我认为函数指针的更常见用法是概括函数调用.例如,如果有一个名为sum_gen(a,b)的函数又可能需要调用具有相似类型的iself()函数或isquare(),那么我们将要做的是,我们将向sum_gen添加一个函数指针参数()函数类似:-
sum_gen(a,b,int(* fun)(void)),其中fun可以采用iself()函数或isquare函数的地址.
现在我们可以像这样调用sum_gen()函数:-
sum_gen(a,b,iself)或sum_gen(a,b,isquare).因此,它基本上用于泛化函数调用.

它们可用于创建跳转表".
枚举{ADD,SUB,MUL,DIV};
double(* fptrs [])(double,double)= {add,sub,mul,divi};
(* fptrs [运算符])(op1,op2);
跳转表可能比switch语句更有效,更优雅.

另一个用途是,当我们要一个接一个地调用某些函数时,在这种情况下,我们声明一个初始化为各个函数的函数指针数组,例如:-
unsigned int i;
int(* fun [])(void)= {fun1,fun2,fun3,fun4,fun5,fun6};
for(i = 0; i<(sizeof(fun)/sizeof(fun [0])); i ++)
(* fun [i])();
这样,将依次调用所有函数,而不是一个一个地调用每个函数.
请注意,使用(* f)()和f()进行调用(其中f是指向函数的指针)在ANSI-C中是相同的.如果f()是一个返回指向函数的指针的函数,则可能会有f()().



希望这对您有所帮助,让我知道.
One more answer

One of the big uses for function pointers in C is to call a function defined at run-time. For example, the C run-time library has two routines, qsort and bsearch, which take a pointer to a function that it calls to compare two items being sorted; this allows you to sort or search, respectively, anything, based on any criteria you wish to use.
I think the more common use of a function pointer is to generalise function calls. e.g., if there is one function called sum_gen(a, b) which in turn may require to call iself() function or isquare() which are of similar types then what we will do, we will add one function pointer argument to the sum_gen() function like:-
sum_gen(a, b, int (*fun)(void)), where fun can take either the address of iself() function or isquare function.
Now we can call sum_gen() function like:-
sum_gen(a, b, iself) or sum_gen(a, b, isquare). So its basically used to generalise the function calls.

They can be used to create ''jump table''.
enum { ADD,SUB,MUL,DIV };
double (*fptrs[])(double,double)={ add, sub, mul, divi };
(*fptrs[ operator ] ) (op1,op2);
Jump table might be more efficient and elegant than switch statement.

The other use is, when we want to call some functions sequentially one by one, in that case we declare one array of function pointers initialised to the respective functions like:-
unsigned int i ;
int (*fun[]) (void) = { fun1, fun2, fun3, fun4, fun5, fun6 };
for( i = 0; i < (sizeof (fun) / sizeof (fun[0])); i++)
(*fun [ i ])();
in this way all the function will be called sequentially instead of calling each function one by one.
Note that calling using (*f)() and f() where f is pointer to function is the same in ANSI-C. You might have f()() if f() is a function that returns pointer to function.



Hope this will we useful for you to understand do let me know.


在C ++中,实际上(使用双关语)没有使用函数指针的实用程序.函数指针可以做的所有事情都应该考虑使用接口指针或仿函数.

原因是使用函子或对象更容易对状态进行分区.函数不能随身携带状态-至少以一种容易允许该状态的多个副本的方式.

干杯,



PS:这不适用于成员函数指针,它们对于执行双重分派和其他必须依赖两种类型的事情非常有用.
In C++ there''s virtually (pardon the pun) no utility in using function pointers. Everything you can do with a function pointer you should consider using an interface pointer OR a functor instead.

The reason is that it''s easier to partition state using a functor or object. Functions cannot carry state around with them - at least in a way that easily allows multiple copies of that state.

Cheers,

Ash

PS: This doesn''t apply to member function pointers, they''re very useful for doing double dispatch and other things that have to depend on two types.


这篇关于功能指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆