C++:How to pass reference-to-function into another function?

后端 未结 1 1943
太阳男子
太阳男子 2021-01-02 04:34

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 reference without using p

1条回答
  •  离开以前
    2021-01-02 05:16

    #include 
    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 );
    }
    

    Cheers & hth.,

    0 讨论(0)
提交回复
热议问题