c++ 回调函数使用

普通回调

    #include<stdio.h> void printWelcome(int len)  
    {  
           printf("welcome -- %d\n", len);  
    } void printGoodbye(int len)  
    {  
           printf("byebye-- %d\n", len);  
    } void callback(int times, void (* print)(int))  
    { int i; for (i = 0; i < times; ++i)  
           {  
                  print(i);  
           }  
           printf("\n welcome or byebye !\n");  
    } void main(void)  
    {  
           callback(10, printWelcome);  
           callback(10, printGoodbye);  
    } 
Top

类成员函数回调

    #include <iostream> #include <functional> using namespace std; using namespace std::placeholders;  
      
    typedef std::function<void(int,int)> Fun; class B{ public: void call(int a,Fun f)  
            {  
                f(a,2);  
            }  
    }; class Test{ public: void callback(int a,int b)  
        {  
            cout<<a<<"+"<<b<<"="<<a+b<<endl;  
        } void bind()  
        {  
            Fun fun=std::bind(&Test::callback,this,_1,_2);  
            B b;  
            b.call(1,fun);  
        }  
      
    }; int main()  
    {  
        Test test;  
        test.bind(); return 0;  
    } 
Top

bind函数

一般常用语法是: newFunName=bind(oldFunName,arg_list);

bind函数返回一个新的函数对象。其中bind第一个参数是oldFunName,它是待绑定的函数名,arg_list是oldFunName的参数列表。注意,这个参数列表是旧函数的参数列表,前面提到,返回的是子函数。我们可以随便给子函数定几个参数,但是肯定不能多于bind所绑定的原函数的参数个数。举个例子:
//g是一个有两个参数的可调用对象  auto g=bind(f,a,b,_2,c,_1); //其中f是具有5个参数的函数 //当我们调用g(x,y)时,实际调用的是f(a,b,y,c,x)

上面出现的_1,_2是它的占位符,bind最多可以使用9个占位符。这个占位符命名在std的placeholders中,使用时,要使用using std::placeholders.

Top

function函数

function是一个函数对象的“容器”。

如function<int(int,int)> fun;  fun是一个函数模板,可以接受两个int型参数,并返回一个int型参数。平时可以将它赋值给一个函数指针。
Top

又一个栗子

    #include <iostream> #include <functional> using namespace std;    
        
    typedef std::function<void ()> fp; void g_fun()    
    {    
        cout<<"g_fun()"<<endl;    
    } class A    
    { public: static void A_fun_static()    
        {    
            cout<<"A_fun_static()"<<endl;    
        } void A_fun()    
        {    
            cout<<"A_fun()"<<endl;    
        } void A_fun_int(int i)    
        {    
            cout<<"A_fun_int() "<<i<<endl;    
        } //非静态类成员,因为含有this指针,所以需要使用bind  void init()    
        {    
            fp fp1=std::bind(&A::A_fun,this);    
            fp1();    
        } void init2()    
        {    
            typedef std::function<void (int)> fpi; //对于参数要使用占位符 std::placeholders::_1  fpi f=std::bind(&A::A_fun_int,this,std::placeholders::_1);    
            f(5);    
        }    
    }; int main()    
    { //绑定到全局函数  fp f2=fp(&g_fun);    
        f2(); //绑定到类静态成员函数  fp f1=fp(&A::A_fun_static);    
        f1();    
        
        A().init();    
        A().init2(); return 0;    
    } 

 

参考于:

http://blog.csdn.net/hyp1977/article/details/51784520

本博客所有文章如无特别注明均为原创。作者:小乐复制或转载请以超链接形式注明转自 众众帮
原文地址《c++ 回调函数使用
分享到:更多

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)