c++ - 函数调用运算符

标签 c++ stl operator-overloading function-call-operator

<分区>

Possible Duplicates:
C++ Functors - and their uses.
Why override operator() ?

我见过在 STL 容器上使用 operator() 但它是什么以及什么时候使用它?

最佳答案

该运算符将您的对象变成仿函数。 Here is nice example of how it is done.

下一个示例演示如何实现一个类以将其用作仿函数:

#include <iostream>

struct Multiply
{
    double operator()( const double v1, const double v2 ) const
    {
        return v1 * v2;
    }
};

int main ()
{
    const double v1 = 3.3;
    const double v2 = 2.0;

    Multiply m;

    std::cout << v1 << " * " << v2 << " = "
              << m( v1, v2 )
              << std::endl;
}

关于c++ - 函数调用运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4689430/

相关文章:

c++ - mingw g++ vector<T>::插入错误

C++ 运算符和参数

python - 你能为 Python 中的重载运算符施加对象优先级吗?

c++ - std::thread::join 是否保证写入可见性

c++ - 有效地从 std::list 中删除最后一个元素

collections - MFC 容器中的 find_if 具有从 std::iterator 派生的迭代器

c++ - 自制 vector 类的重载 = 运算符

c++ - QML Coverflow 非常慢

C++通过范围for循环将整数输入 vector ;随机 0

c++ - 引用数据成员更改 const 函数内的另一个数据成员