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;    
    }  
复制代码









本文转自 jiu~ 博客园博客,原文链接:http://www.cnblogs.com/jiu0821/p/8464605.html,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
存储 JSON 安全
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
66 0
|
23天前
|
人工智能 机器人 中间件
【C++】C++回调函数基本用法(详细讲解)
【C++】C++回调函数基本用法(详细讲解)
|
3月前
|
C++
c++将一个类的回调函数注入到另一个类中的方法
c++将一个类的回调函数注入到另一个类中的方法
|
4月前
|
消息中间件 存储 API
【C/C++】回调函数详解&注册窗口类&LRESULT & CALLBACK详解以及游戏中的应用
【C/C++】回调函数详解&注册窗口类&LRESULT & CALLBACK详解以及游戏中的应用
53 0
|
10月前
|
搜索推荐 Java C语言
c++回调函数详解及实现(lambda)
c++回调函数详解及实现(lambda)
|
5月前
|
C++
《C++避坑神器·二十一》回调函数使用
《C++避坑神器·二十一》回调函数使用
57 0
|
6月前
|
API C++
回顾C++回调函数
回顾C++回调函数
|
10月前
|
Java 数据处理 C++
c++11线程池的实现原理及回调函数的使用
c++11线程池的实现原理及回调函数的使用
|
10月前
|
C++
「C/C++」C/C++ 回调函数
「C/C++」C/C++ 回调函数
|
消息中间件 存储 设计模式
【C/C++】回调函数详解&注册窗口类&LRESULT & CALLBACK详解以及游戏中的应用
【C/C++】回调函数详解&注册窗口类&LRESULT & CALLBACK详解以及游戏中的应用
300 0
【C/C++】回调函数详解&注册窗口类&LRESULT & CALLBACK详解以及游戏中的应用