运算符()的含义?
C++中operator()的含义是什么?我发现它经常用于“函子”或函数对象,例如比较器。那么这些函数是如何调用的呢?在其他情况下有用吗?我可以为这样的运算符声明多少个参数?例如,以下内容可以接受吗?
bool operator() (Foo f, Baz b, Quz q, Oik o) {...}
What is the meaning of operator() in C++? I see that is often used for "functors," or function objects such as comparators. But then how are such functions called? Is it useful in other situations? And how many parameters can I declare for such an operator? E.g., is the following acceptable?
bool operator() (Foo f, Baz b, Quz q, Oik o) {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您有一个定义了
operator()
的函数对象类Func
。如果您有该类的实例,则只需在引用该实例的表达式后面放置括号即可:作为标准库中的示例,我们可以看一下
std::less
,它是一个二进制函数object:operator()
的另一个常见用途是创建 Matrix 类。您可以定义T& Matrix::operator()(int,int)
从矩阵中检索元素,如myMatrix(1,2)
。operator()
可以采用的参数数量与任何其他函数相同。但这是实现定义的。您的实现应允许的建议参数最小数量为 256 个(在标准附录 B 中给出)。operator()
的查找在标准 (ISO/IEC 14882:2003 §13.3.1.1.2) 中定义为:翻译:如果使用语法
expression()
进行函数调用,并且括号之前的表达式计算为类的实例,则添加对象operator()
成员function 到可以调用的候选函数列表。Let's say you have a function object class,
Func
, withoperator()
defined. If you have an instance of that class, you can simply place parentheses after the expression referring to that instance:As an example from the standard library, we can look at
std::less
which is a binary function object:Another common use for
operator()
is when creating a Matrix class. You may defineT& Matrix::operator()(int,int)
to retrieve an element from the matrix likemyMatrix(1,2)
.The number of parameters that
operator()
can take is the same as any other function. This is implementation-defined though. The recommended minimum number of arguments that your implementation should allow is 256 (given in Annex B of the standard).operator()
's lookup is defined in the standard (ISO/IEC 14882:2003 §13.3.1.1.2) by:Translation: If you make a function call using the syntax
expression()
and the expression before the parentheses evaluates to an instance of a class, then add the objectsoperator()
member function to the list of candidate functions that may be called.如果您为类
C
实现operator()
,那么如果c
是C
类型的对象,您可以编写c()
,它调用c.operator()()
。您可以为operator()
添加任意数量的参数。有关
operator()
何时有用的一个很好的示例,请参见此处:http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10
If you implement
operator()
for a classC
, then ifc
is an object of typeC
you can writec()
, which invokesc.operator()()
. You can have as many parameters tooperator()
as you like.For one good example of when
operator()
is useful, see here:http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10
我没有看到提到的一点是,尽管在语法上它的调用看起来与常规非成员(我敢说,C 风格)函数非常相似。一个区别是,由于函子/函数对象是类/结构的实例,因此它可以具有关联的状态。
对于非成员函数,这通常意味着静态局部变量(通常不是维护状态的最佳或最有用的方法)或全局变量(通常是不可取的)。而函子/函数对象与 C++ 的封装概念很好地结合在一起,其中状态/数据与对其进行操作的函数紧密相关。
一个有启发性的例子是:
One point which I didn't see mentioned, was that although in syntax its invocation looks very similar to that of a regular non-member (dare I say, C-style) function. One difference is that since a functor/function-object is an instance of a class/struct, it can have an associated state.
For non-member functions this could usually mean, either static locals(often times not the best or most useful way to maintain state) or globals(often undesirable). Whereas functors/function objects tie in nicely with C++'s notions on encapsulation where in the state/data is closely tied to the functions that operate on it.
An instructive example of this is:
与任何重载运算符一样,
operator()
是一个看起来很奇怪的函数名称。它必须是类或结构的成员函数,但除了所有函数的通常规则之外,对其参数没有任何限制。当
x
的类型是类类型时,要计算表达式x(
args)
,C++ 将尝试调用 x.operator()(args)
,其中 args 可以是空字符串或任意数量的函数论据。Like any overloaded operator,
operator()
is a strange-looking name of a function. It must be a member function of a class or struct, but there are no restrictions on its arguments other than the usual rules for all functions.To evaluate the expression
x(
args)
when the type ofx
is a class type, C++ will attempt to callx.operator()(
args)
, where args can be the empty string or any number of function arguments.它只是一个普通函数,当您将函数调用语法应用于用户定义类型时,就会调用该函数。
因此它具有常规成员函数的所有限制和功能。它可以是一个模板,它可以接受任意数量的参数,它需要一个返回类型,它可以是
const
等。It's just a normal function that gets called when you apply function-call syntax to a user-defined type.
So it has all the restrictions and features of a regular member function. It can be a template, it can take as many parameters as you want, it needs a return type, it can be
const
, etc.