zoukankan      html  css  js  c++  java
  • 注册回调

    背景

      总结一下在c,c++里注册回调的几种用法

    用法

    c语言

    #include <iostream>                                                                                                                   
    
    using namespace std;
    
    int demo(char *str1, char *str2)
    {
        printf("print in demo(): %s, %s
    ",
                str1, str2);
        return 0;
    }
    
    int main()
    {
        //指针类型定义
        typedef int (*FuncType)(char*, char*);
    
        //函数指针定义
        FuncType func = demo;
    
        //调用
        func("hello", "world");
    }

    boost::function

    #include <iostream>
    #include "boost/function.hpp"
    
    int demo(char *str1, char *str2)
    {
        printf("print in demo(): %s, %s
    ",
                str1, str2);
        return 0;
    }
                                                                                                                                          
    int main()
    {
        boost::function<int(char*, char*)>  func(demo);
        func("111", "222");
    }

    boost::bind

    #include <iostream>
    #include "boost/thread.hpp"
    
    using namespace std;
    
    int demo(char *str1, char *str2)
    {
        sleep(2);//two seconds
        printf("print in demo(): %s, %s
    ",
                str1, str2);
        return 0;
    }
                                                                                                                                          
    int main()
    {
        boost::thread mythread(boost::bind(&demo, "1", "2"));
    
        mythread.join();
        printf("thread over
    ");
    }

    std::function (c++11)

    #include <iostream>                                                                                                                   
    #include <functional>
    
    using namespace std;
    
    int demo(char *str1, char *str2)
    {
        printf("print in demo(): %s, %s
    ",
                str1, str2);
        return 0;
    }
    
    int main()
    {
        function<int(char*, char*)> func = demo;
        func("111", "222");
    }

    std::function (c++11) 绑定类静态成员函数

    #include <iostream>
    #include <functional>
    
    using namespace std;
    
    class demo
    {
    public:
        static int demo_func(char *str)
        {   
            printf("print in demo_func: %s
    ", str);
            return 0;
        }   
    };
    
    int main()
    {
        function<int(char*)> func = demo::demo_func;
        func("hello world");
    }

    std::function (c++11) 绑定函数对象或仿函数

    #include <iostream>                                                                                                                   
    #include <functional>
    
    using namespace std;
    
    class Foo {
    public:
        int operator()(char *str1, char *str2)
        {   
            printf("print in demo(): %s, %s
    ",
                    str1, str2);
            return 0;
        }   
    };
    
    int main()
    {
        Foo foo;
        function<int(char*, char*)> func;
        func = foo;
        func("111", "222");
    }
  • 相关阅读:
    mongodb基础系列——数据库查询数据返回前台JSP(一)
    整型数组处理算法(十二)请实现一个函数:最长顺子。[风林火山]
    学习C++服务端一:MySql与C++
    【算法】深度优先搜索(DFS)III
    DOS cmd
    C# wpf程序获取当前程序版本
    Climbing Stairs
    Java深入
    非对称算法,散列(Hash)以及证书的那些事
    省市区镇(能够选四级)联动点击自己主动展开下一级
  • 原文地址:https://www.cnblogs.com/orejia/p/12112737.html
Copyright © 2011-2022 走看看