如何在函数中传递函数? [英] How to pass a function in a function?

查看:87
本文介绍了如何在函数中传递函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奇怪的标题.如果有人能澄清我到底在问什么,我将不胜感激,因为我自己也不太确定.

That's an odd title. I would greatly appreciate it if somebody could clarify what exactly I'm asking because I'm not so sure myself.

我正在看斯坦福关于编程范式的视频(那个老师很棒),当他开始这样做时,我正在观看视频五:

I'm watching the Stanford videos on Programming Paradigms(that teacher is awesome) and I'm up to video five when he started doing this:

void *lSearch( void* key, void* base, int elemSize, int n, int (*cmpFn)(void*, void*))

自然地,我心里想,哎呀,我不知道你可以声明一个函数,然后再定义它!".所以我创建了自己的 C++ 测试版本.

Naturally, I thought to myself, "Oi, I didn't know you could declare a function and define it later!". So I created my own C++ test version.

int foo(int (*bar)(void*, void*));
int bar(void* a, void* b);

int main(int argc, char** argv)
{
    int *func = 0;
    foo(bar);

    cin.get();
    return 0;
}

int foo(int (*bar)(void*, void*))
{
    int c(10), d(15);
    int *a = &c;
    int *b = &d;
    bar(a, b);
    return 0;
}

int bar(void* a, void* b)
{
    cout << "Why hello there." << endl;
    return 0;
}

关于代码的问题是:如果我将函数int *bar 声明为foo 的参数,而不是int (*bar),它会失败.为什么!?

The question about the code is this: it fails if I declare function int *bar as a parameter of foo, but not int (*bar). Why!?

此外,视频让我感到困惑,因为他的 lSearch 定义

Also, the video confuses me in the fact that his lSearch definition

void* lSearch(/*params*/, int (*cmpFn)(void*, void*)) 在定义中调用的是cmpFn,但是在调用lSearch函数时

void* lSearch( /*params*/ , int (*cmpFn)(void*, void*)) is calling cmpFn in the definition, but when calling the lSearch function

lSearch( /*params*/, intCmp );

还调用定义的函数 int intCmp(void* elem1, void* elem2); 但我不明白它是如何工作的.为什么在 lSearch 中,函数名为 cmpFn,但定义为 intCmp,类型为 int,而不是 int* 并且仍然有效?为什么 lSearch 中的函数不必定义参数?

also calls the defined function int intCmp(void* elem1, void* elem2); and I don't get how that works. Why, in lSearch, is the function called cmpFn, but defined as intCmp, which is of type int, not int* and still works? And why does the function in lSearch not have to have defined parameters?

推荐答案

int (*cmpFn)(void*, void*) is a function pointer -- a指向以后可以调用的函数的指针.当您调用 iSearch 时,您向它传递了一个函数,该函数接受两个 void* 并返回一个 int,并将其绑定到参数 cmpFn.然后 iSearch 可以做一些类似 int x = cmpFn(voidPtr1, voidPtr2); 的事情来调用这个函数,传递它 voidPtr1voidPtr2 作为它的参数并将返回值存储在 x

int (*cmpFn)(void*, void*) is a function pointer -- a pointer to a function that can be called later. When you call iSearch you pass it a function that takes two void* and returns an int, and it binds that to the parameter cmpFn. Then iSearch can do something like int x = cmpFn(voidPtr1, voidPtr2); to call that function, passing it voidPtr1 and voidPtr2 as its arguments and storing the return value in x

您可以通过声明一个函数指针并在同一函数中使用它来尝试一个简单的示例:

You can try a simple example by just declaring a function pointer and using it in the same function:

int test1(int x) {return x;}
int test2(int x) {return x+1;}

int main(int argc, char** argv) {
    int (*fn)(int); // Function pointer named 'fn' that can hold a function that takes one int argument and returns an int
    int rtn;

    fn = test1; // Assign the 'test1' function to 'fn'
    rtn = fn(4); // Call 'fn' ('test1') with the argument 4; it returns 4 and stores it in 'rtn'

    fn = test2; // Assign the 'test2' function to 'fn'
    rtn = fn(4); // Call 'fn' ('test2') with the argument 4; it returns 5 and stores it in 'rtn'
}

如果你声明 int *bar 会失败,因为它不是一个函数指针,它只是一个指向整数的指针.函数指针的语法是 rtn_type (*name)(param1_type, param2_type, ...)

It fails if you declare int *bar because that's not a function pointer, it's just a pointer to an integer. The syntax for function pointers is rtn_type (*name)(param1_type, param2_type, ...)

这篇关于如何在函数中传递函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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