<分区>
Possible Duplicates:
C++ Functors - and their uses.
Why override operator() ?
我见过在 STL 容器上使用 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/