zoukankan      html  css  js  c++  java
  • 函数对象function object 以及boost::bind的一点了解

    1 function object就是重载了operator()的类的对象, 像如下例子, 其实也许应该说是定义了operator()的类, 在thinking in c++中operator overloading都并没有提到operator()

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 class Wy {
     5 public :
     6     string operator()(string msg, int a, int b){
     7         cout << "msg = " << msg << endl;
     8         cout << "a = " << a << "  b = " << b << endl;
     9         return msg;
    10     }
    11 };
    12 int main(){
    13     Wy wy;
    14     string msg = wy("test", 2, 3);
    15     cout << "in main msg = " << msg << endl;
    16     return 0;
    17 }

    可以看到 operator()(...),可以有任意个参数, 和任意类型返回值

    2. STL中的一个头文件<functional>  http://www.cplusplus.com/reference/std/functional/

    标准库在这个头文件中提供了两个base class :   unary_function和binary_function, 这两个class 模板只是把template参数的名字, 也就是类型名, 统一化而以,  这个头文件中定义了很多operation class,就是定义了operation()的类, 我们可以把它用在很多STL algorithm中, 这些operation类都从两个base class之一派生, 从两个base class之一派生就是为了让function object具有可适配的特性, 就是可以 bind什么的, boost::bind用这样一个名称集合了STL的functional提供的所有可让普通函数, 成员函数变为function object中的operator()来调的方法

    template<class Arg, class Result>
    struct unary_function{
             typedef Arg argument_type;
             typedef Result result_type;   
    };
    template<class Arg1, class Arg2, class Result>
    struct binary_function{
             typedef Arg1 first_argument_type;
             typedef Arg2 second_argument_type;
             typedef Result result_type;
    };
    

     列出<functional>中greater的定义, 可以看看模板类又带继承是怎么写的

    template<class T>
    struct greater : public binary_function<T, T, bool>{
             bool operator(const T& a, const T&b) const{
                     return a>b;
             }
    } ;
    

      

    3. boost::thread对像在构建时要传入一个 object of callable type that can be invoked with no parameters to the constructor(callable就是要定义operator()), 也就是有默认构造函数的函数对象, 以下是我写的一个例 子

     1 //#include <unistd.h>
     2 #include <iostream>
     3 #include <string>
     4 #include <boost/thread.hpp>
     5 using namespace std;
     6 class Wy{
     7 public :
     8     string operator()(string msg, int a, int b){
     9         for (int i=0; i<15; i++){
    10             cout << "thread2 : msg = " << msg << endl;
    11             cout << "a = " << a << "  b = " << b << endl;
    12             sleep(1);
    13         }
    14         return msg;
    15     }
    16     void operator()(){
    17         for (int i=0; i<10; i++){    
    18             cout << "thread1: msg in operator() " << endl;
    19             sleep(2);
    20         }
    21     }
    22 };
    23 int main(){
    24     Wy wy;
    25     boost::thread thread1(wy);
    26     boost::thread thread2(boost::bind<string>(wy, "test2", 5, 6));
    27     thread1.join();
    28     cout << "thread1 end " << endl;
    29     thread2.join();
    30     return 0;
    31 }

    运行结果比较长,我就不列出来了,仔细看看这个程序做了什么事,就是在main中启动了两个线程,线程执行的函数分别是类Wy的两个operator()成员。 

    上面的程序编译的时候要链接libboost_thread,在我本地是这么做的:g++ -o operator_call operator_call.cpp -lboost_thread  -L/usr/local/lib  

    第1行被注掉了,但sleep(2)依然可以被调用,休两秒,不明白是为什么,boost::thread中也有sleep的函数,只是参数也是库中定义的类型;  25,26行构造了两个thread对象,代表两个线程,传进去的参数都是function object,注意26行boost::bind是怎么使用function object的,指明了返回值,对象本身,和依次调用的参数,这个参见boost::bind文档;   在27行thread1.join()等待thread1退出, 由于thread2会先于thread1退出,所以程序在28行打印"thread1 end"时thread2已经退出了

    引发这个思考的是在talk工程的main函数中,分出了一个线程运去行TalkServer.start(), 主线程是运行了一个Ice adapter,里面添加了相应的objectPtr, 运行了具本的ice服务, 在启动这个线程是这么做的  

    1 boost::thread thread(bind(&TalkServer::start, &talkServer));

     talkServer是之前定义的一个引用,这引出下面一般性的讨论

    4,  use boost::bind with pointers to members    http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#with_function_objects

    这个地方我也没有特别完全理解,现在只列出一基本的知道的,   bind(&X::f, args)相当于  bind<R>(mem_fn(&X::f), args), 而 mem_fn是boost对于std::mem_fun和std::mem_fun_ref(这义于std的<functional>)的一般性扩展,   mem_fun构建出function object,并且其args第一个参数要是成员所属的那个对象的pointer, reference, 或smart_ptr(持有这个类对象), 像上面我用的就是talkServer的pointer(但我在talk中把这里的pointer改为reference, talkServer本身就是个reference,编译出错)

  • 相关阅读:
    黑马程序员JAVA高级视频_IO输入与输出18天2(FileWriter)
    黑马程序员JavaAPI17天5(集合转成数组)
    黑马程序员JAVA高级视频_IO输入与输出19天4(MyBufferedReader)
    android 取消webview的背景色
    DLNA
    Android JNI的若干问题总结
    gcc 一些应用
    如何基于nand flash启动Linux内核(分享一段实用、简单、类似bootloader功能的代码)
    Android JNI开发高级篇有关Android JNI开发中比较强大和有用的功能就是从JNI层创建、构造Java的类或执行Java层的方法获取属性等操作。 一、类的相关操作 1. jclass FindClass(JNIEnv *env, const char *name);
    ubuntu 配置ndk
  • 原文地址:https://www.cnblogs.com/livingintruth/p/2506084.html
Copyright © 2011-2022 走看看