Function Pointers, Functors, Virtual Functions

来源:互联网 发布:js头像上传裁剪插件 编辑:程序博客网 时间:2024/05/22 05:39

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.

 

相关文章url:

http://www.cnblogs.com/rogerroddick/category/443377.html

原创粉丝点击