运算符重载 C++
我在互联网上找到了这段代码:
Class Book{
Public:
void operator()(int Counter) const throw();
}
我的问题是,使用什么运算符重载上述代码?
I found this code on internet :
Class Book{
Public:
void operator()(int Counter) const throw();
}
My question is, what operator overloading the above code used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,该代码是错误的;由于 C++ 区分大小写,因此
Class
和Public
不是关键字。将参数名称的首字母大写 (Counter
) 也是很不寻常的(尽管合法)。假设大小写正确,您所拥有的是函数调用运算符的重载。它允许您“调用”
Book
的实例,就像它是一个函数一样:Firstly, that code is wrong; since C++ is case sensitive,
Class
andPublic
are not keywords. It is also very unusual (albeit legal) to capitalize the first letter of a parameter name (Counter
).Assuming correct capitalization, what you have is an overload of the function-call operator. It allows you to "call" an instance of
Book
as if it was a function:上面的类基本上称为“函子”。它有一个重载的“()”运算符。广泛应用于STL算法。
The above class is basically called a "Functor". It has an overloaded "()" operator. Widely used in STL Algorithms.