函数调用运算符
我见过operator()的使用
在 STL 容器上,但它是什么以及什么时候使用它?
Possible Duplicates:
C++ Functors - and their uses.
Why override operator() ?
I've seen the use of operator()
on STL containers but what is it and when do you use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该运算符将您的对象变成函子。 这是一个很好的示例,说明了如何完成此操作。
下一个示例演示如何实现一个类并将其用作函子:
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 :
它使对象像函数一样“可调用”。但与函数不同的是,对象可以保存状态。实际上,函数可以使用静态局部变量在弱意义上执行此操作,但是对于任何线程在任何上下文中对该函数进行的任何调用,该静态局部变量都会永久存在。
对于充当函数的对象,状态仅是该对象的成员,并且同一类的其他对象可以拥有自己的成员变量集。
整个 boost::bind (基于旧的 STL 绑定器)都是基于这个概念。
该函数具有固定签名,但通常您需要的参数多于签名中实际传递的参数才能执行操作。
It makes the object "callable" like a function. Unlike a function though, an object can hold state. Actually a function can do this in a weak sense, using a static local, but then that static local is permanently there for any call to that function made in any context by any thread.
With an object acting as a function, the state is a member of that object only and you can have other objects of the same class that have their own set of member variables.
The entirety of boost::bind (which was based on the old STL binders) is based on this concept.
The function has a fixed signature but often you need more parameters than are actually passed in the signature to perform the action.