函数调用运算符 [英] Function call operator

查看:69
本文介绍了函数调用运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
C ++函子-及其用途.
为什么要覆盖operator()?

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

我已经在STL容器上看到了operator()的用法,但是它是什么以及何时使用呢?

I've seen the use of operator() on STL containers but what is it and when do you use it?

推荐答案

该运算符将您的对象变成函子. 这是一个很好的示例.

That operator turns your object into functor. Here is nice example of how it is done.

下一个示例演示如何实现将类用作函子的类:

Next example demonstrates how to implement a class to use it as a functor :

#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;
}

这篇关于函数调用运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆