zoukankan      html  css  js  c++  java
  • C++ Functor

    Functors Compared with Function Pointers

    If you have a function that takes a function pointer, you cannot pass in a functor as though it were a function pointer, even if the functor has the same arguments and return value as the function pointer.
    Likewise, if you have a function that expects a functor, you cannot pass in a function pointer.

    Functors, Function Pointers, and Templates

    If you wish to allow either a function pointer or a functor to be passed into the same function, you need to use templates. The templated function will deduce the proper type for the functor or function pointer, and both functors and function pointers are used in the exact same way--they both look like function calls--so the code in the function will compile fine.
    For instance, let's say you had this simple little find_matching_numbers function that takes a vector and returns a vector of all numbers for which a given function returns true.


    int g_Add(int lhs, int rhs)
    {
    return lhs + rhs;
    }

    class Functor
    {
    public:
    int operator ()(int lhs, int rhs)
    {
    return lhs + rhs;
    }
    };

    void Call(BinaryOp op, int arg1, int arg2)
    {
    cout<<op(arg1, arg2)<<endl;
    }
    void Call1(Functor op, int arg1, int arg2)
    {
    cout<<op(arg1,arg2)<<endl;
    }
    void Call2(int (*op)(int,int), int arg1, int arg2)
    {
    cout<<op(arg1, arg2)<<endl;
    }
    template <typename Func>
    void Call3(Func op, int arg1, int arg2)
    {
    cout<<op(arg1,arg2)<<endl;
    }
    int main()
    {
    BinaryOp funp = g_Add;
    Functor functor;
    Call(funp, 1,1);
    //Call1(funp,1,1);
    //Call(functor,1,1);
    //Call2(functor, 1,1);
    Call3<Functor>(functor, 1,1);
        Call3<BinaryOp>(funp, 1,1);
    return 0;
    }
    Functors vs. Virtual Functions

    Functors and virtual functions are, believe it or not, closely related. They both solve the same problem: how do we let some code choose the algorith that is applied. Imagine that you have a function called doMath, and it takes 3 arguments: two integers, and a "kind of math to do". You could write it with functors like this:

    template <FuncType>
    int doMath (int x, int y, FuncType func)
    {
    return func( x, y );
    }

    Another way that you could write this is by creating an interface class that has a computeResult method:

    class MathComputer
    {
        virtual int computeResult (int x, int y) = 0;
    };
     
    doMath (int x, int y, MathComputer* p_computer)
    {
        return p_computer->computeResult( x, y );
    }

    Admittedly, this is kind of a boring example! All it shows you is that both virtual functions and functors allow you to dynamically choose the exact algorithm that is executed.
    The major difference is that using virtual functions does not give the ability to write a templated function that can also accept function pointers. Depending on your application, this may be important: if you're writing a library, it's probably very important; if you're writing a small program for yourself, it's likely less important. A virtual function, on the other hand, has somewhat simpler syntax (no complex templates!) and tends to fit the normal object-oriented programming mindset.

  • 相关阅读:
    Win10 企业版ltsc 无法访问samba网络共享问题及解决!(转)
    内核发送uevent的API,用户空间解析uevent(转)
    修改kile工程名字(转)
    gdb调试
    当usbnet打印 kevent * may have been dropped(转)
    收藏一份devmem源码
    Linux Performance
    控制 input框只输入数字
    springboot环境中,可能会出现使用font-Awesome结果图标不显示的问题,在webService的pom文件中添加如下配置代码
    js防止页面抖动(按钮,请求等重复提交)
  • 原文地址:https://www.cnblogs.com/rogerroddick/p/3074398.html
Copyright © 2011-2022 走看看