开发者

How to pass reference-to-function into another function

I have been reading about function pointers and about using them as parameters for other functions.

My question is how would you pass a function by re开发者_运维问答ference without using pointers? I have been trying to find the answer on the Internet but I haven't found a good answer.

I know that you can pass variables by reference like this: void funct(int& anInt);. How would you do something similar to this, but instead of a reference to a variable, a reference to a function was the parameter?

Also, how would you use a reference to the function in a function body?


#include <iostream>
using namespace std;

void doCall( void (&f)(int) )
{
    f( 42 );
}

void foo( int x )
{
    cout << "The answer might be " << x << "." << endl;
}

int main()
{
    doCall( foo );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜