函数对象

来源:互联网 发布:php在线调试工具 编辑:程序博客网 时间:2024/06/01 11:11

转自:http://www.cnblogs.com/ly4cn/archive/2007/07/21/826885.html

函数对象不是函数指针。但是,在程序代码中,它的调用方式与函数指针一样,后面加个括号就可以了。
  这是入门级的随笔,说的是函数对象的定义,使用,以及与函数指针,成员函数指针的关系。 
函数对象实质上是一个实现了operator()--括号操作符--的类。
例如:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->class Add{public:  int operator()(int a, int b)  {    return a + b;  }};

 

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->
Add add; // 定义函数对象cout << add(3,2);// 5

函数指针版本就是:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->
int AddFunc(int a, int b){  return a + b;}typedef int (*Add) (int a, int b);

 

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->
Add add = &AddFunc;//AddFunc亦可用cout << add(3,2);// 5

除了定义方式不一样,使用方式可是一样的。都是:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->cout << add(3,2);

既然函数对象与函数指针在使用方式上没什么区别,那为什么要用函数对象呢?很简单,函数对象可以携带附加数据,而指针就不行了。
下面就举个使用附加数据的例子:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->class less{public:    less(int num):n(num){}    bool operator()(int value)    {        return value < n;    }private:    int n;};


使用的时候:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)--> less isLess(10);cout << isLess(9) << " " << isLess(12); // 输出 1 0


这个例子好象太儿戏了,换一个:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->const int SIZE = 5;int array[SIZE] = { 50, 30, 9, 7, 20};// 找到小于数组array中小于10的第一个数的位置int * pa = std::find_if(array, array + SIZE, less(10));// pa 指向 9 的位置// 找到小于数组array中小于40的第一个数的位置int * pb = std::find_if(array, array + SIZE, less(40)); // pb 指向 30 的位置

这里可以看出函数对象的方便了吧?可以把附加数据保存在函数对象中,是函数对象的优势所在。
它的弱势也很明显,它虽然用起来象函数指针,但毕竟不是真正的函数指针。在使用函数指针的场合中,它就无能为力了。例如,你不能将函数对象传给qsort函数!因为它只接受函数指针。
要想让一个函数既能接受函数指针,也能接受函数对象,最方便的方法就是用模板。如:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->template<typename FUNC>int count_n(int* array, int size, FUNC func){    int count = 0;    for(int i = 0; i < size; ++i)        if(func(array[i]))            count ++;    return count;}

这个函数可以统计数组中符合条件的数据个数,如:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->const int SIZE = 5;int array[SIZE] = { 50, 30, 9, 7, 20};cout << count_n(array, SIZE, less(10)); // 2

用函数指针也没有问题:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->bool less10(int v){    return v < 10;}cout << count_n(array, SIZE, less10); // 2

另外,函数对象还有一个函数指针无法匹敌的用法:可以用来封装类成员函数指针!
因为函数对象可以携带附加数据,而成员函数指针缺少一个类实体(类实例)指针来调用,因此,可以把类实体指针给函数对象保存起来,就可以用于调用对应类实体成员函数了。

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->template<typename O>class memfun{public:    memfun(void(O::*f)(const char*), O* o): pFunc(f), pObj(o){}    void operator()(const char* name)    {        (pObj->*pFunc)(name);    }private:    void(O::*pFunc)(const char*);    O* pObj;};class A{public:    void doIt(const char* name)    { cout << "Hello " << name << "!";}};
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)-->   A a;memfun<A> call(&A::doIt, &a); // 保存 a::doIt指针以便调用call("Kitty"); // 输出 Hello Kitty!

大功告成了,终于可以方便保存成员函数指针,以备调用了。
不过,现实是残酷的。函数对象虽然能够保有存成员函数指针和调用信息,以备象函数指针一样被调用,但是,它的能力有限,一个函数对象定义,最多只能实现一个指定参数数目的成员函数指针。
标准库的mem_fun就是这样的一个函数对象,但是它只能支持0个和1个参数这两种成员函数指针。如 int A::func()或void A::func(int)、int A::func(double)等等,要想再多一个参数如:int A::func(int, double),不好意思,不支持。想要的话,只有我们自已写了。
而且,就算是我们自已写,能写多少个?5个?10个?还是100个(这也太恐怖了)?
好在boost库提供了boost::function类,它默认支持10个参数,最多能支持50个函数参数(多了,一般来说这够用了。但它的实现就是很恐怖的:用模板部份特化及宏定义,弄了几十个模板参数,偏特化(编译期)了几十个函数对象。

----
C++0x已经被接受的一个提案,就是可变模板参数列表。用了这个技术,就不需要偏特化无数个函数对象了,只要一个函数对象模板就可以解决问题了。期待吧。