函子 - 它们相对于未实现运算符的其他类具有什么能力 ( )

Functor - what power do they have over other classes that didn't implement operator ( )

本文关键字:什么 能力 其他 相对于 运算符 实现 函子      更新时间:2023-10-16

我试图理解函子的"力量"。

好的,所以它们是指向函数的指针,但是它们能做什么,而其他没有实现operator()的类不能做什么?

例如:

#include <iostream>
#include <assert.h>
#include <vector>
using namespace std;

class MultiplyBy
{
    private:
        int factor;
    public:
        MultiplyBy(int x) : factor(x) {}
        int operator () (int other) const
        {
            return factor * other;
        }
};

int main()
{
    MultiplyBy temp(5);
    int myResult = temp(5);  // now myResult holds 25
    cout << myResult << endl;
    return 0;
}

我们带走他的另一个朋友

class MultiplyOther
{
    private:
        int factor;
    public:
        MultiplyOther(int x) : factor(x) {}
        int invokeMultiplyMe(int _other)
        {
            return _other * factor;
        }
};

并做同样的事情:

int main()
{
    // MultiplyOther
    MultiplyOther notFunctor(4);
    int myOther = notFunctor.invokeMultiplyMe(3);
    cout << myOther << endl;
    return 0;
}

我们得到:

25
12

那么,函子的真正力量是什么?是两个类都保存了状态,还是我在这里遗漏了什么?

首先,它们有一个统一的签名,例如,只需要知道它们必须调用某些东西的算法就可以使用。

在您的示例中,任何期望调用接受int并返回一个(或可从/convertible 构造到 int 的类型(的代码都可以使用 MultiplyBy 的实例。使用第二个示例需要代码了解invokeMultiplyByMe方法。

请注意,函子根本不是指向函数的指针,如问题中所述。它们是类,其实例可以使用函数调用的语法进行调用。

函子对类没有任何特殊属性,但它们可以用作具有更高功能的函数。函数通常不能有状态,如果它有(通过使用静态变量(,则状态在所有函数调用之间共享。

函子具有类的所有属性(尤其是状态(,但可以用作函数。一个很好的例子是用于 STL 算法的函子:

class Add
{
  public:
    Add(){sum = 0;};
    void operator()(int i)
    {
        sum += i;
    }
    int getSum(){return sum;};
    private:
    int sum;
}
int main()
{
std::vector<int> vint;
vint.push_back(2);
vint.push_back(3);
vint.push_back(4);
}
Add adder;
adder = std::for_each( vint.begin(); vint.end(), adder);
cout << adder.getSum();

"所以它们是指向函数的指针" - 函数指针和函子完全不同。前者只是指向原始函数的普通指针。它不持有任何状态。但是,函子或函数对象保存状态。函子是定义运算符((的任何类。因此,该类的任何对象都可以以函数的方式调用。

来自C++入门的代码片段...

template<class T>
class TooBig
{
    private:
    T cutoff;
    public:
    TooBig(const T & t) : cutoff(t) {}
    bool operator()(const T & v) { return v > cutoff; }
};

使用此类,我们可以创建具有不同截止值的多个对象。太大截止1(10(;太大的截断2(20(;

使用函子的最佳位置之一是模板。例如,采用列表模板。它有一个remove_if函数,该函数删除列表中从参数返回 true 的任何值。

list list1;//假设它有一些值列表列表2;假设它有一些值

现在,调用 list1.remove_if(cutoff1(;//应用 list1 的所有 elment,并删除那些大于 cutOff1 中存储的值的 elment。列表 2 类似的事情。

感谢!