我怎样才能“双重超载”?操作员?
如果我有一个类似数据库的类,并且我想做这样的事情:
object[1] == otherObject;
如何“双重重载”运算符[]和运算符==?
If I have a database-like class, and I want to do something like this:
object[1] == otherObject;
How do I "double overload" operator[] and operator==?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
object[1]
返回someType
的对象。您需要在该someType
上重载operator==
。object[1]
returns an object ofsomeType
. You need to overloadoperator==
on thatsomeType
.您所描述的只是两个单独的运算符重载。只需确保
operator ==
的参数与operator []
的返回类型匹配即可。What you describe is simply two separate operator overloads. Just make sure that the parameter to
operator ==
matches the return type ofoperator []
.起初我对问题的措辞感到非常困惑,但现在我想我明白了。你不能直接这样做;但你可以达到同样的效果。这个想法是让
operator[]
返回一个代理对象。对于代理对象,您可以为operator==
提供自定义行为以进行比较。我见过 std::map 的 operator[] 在一些较旧的、更晦涩的标准库实现上以这种方式实现。示例(假设 object[1] 通常返回 Foo&):
注意:在这种情况下您想要重载比较运算符的含义似乎有点奇怪(代理对象通常用于具有关联结构的自定义赋值行为或对于花哨的元模板编程),但为了进行比较,它们通常应该提供与直接使用
Foo
相同的含义,例如尽管如此,这里是实现它的方法:一种通过operator= 获取自定义行为的方法=
仅当在同一表达式中使用重载的operator[]
时才适用。如果你想彻底做到这一点,那么请将 SomeProxy 的构造函数设为私有,并将 Database 设为友元。这样,只有 Database: 可以创建这些对象(在本例中通过运算符[]),并且客户端将无法复制它(他不应该这样做),只能从中获取 Foo 对象/引用或使用表达式中返回的代理。
I was really confused at first by the wording of the question, but now I think I understand. You can't do this directly; but you can achieve the same effect. The idea is to make
operator[]
return a proxy object. For the proxy object, you can provideoperator==
with custom behavior for comparison. I've seen std::map's operator[] implemented this way on some older, more obscure standard library implementations.Example (let's say object[1] normally returns Foo&):
Note: it seems kind of odd that you want to overload the meaning of a comparison operator in such a case (proxy objects are often used for custom assignment behavior with associative structures or for fancy metatemplate programming) but for comparison they should typically provide the same meaning as working with
Foo
directly, e.g. Nevertheless, here's the way to do it: a way to get custom behavior throughoperator==
that is only applicable when using an overloadedoperator[]
in the same expression.If you want to make this thorough, then make the constructors of SomeProxy private and make Database a friend. This way, only the Database: can create those objects (through operator[] in this case) and the client won't be able to copy it (which he shouldn't), only get a Foo object/reference out of it or use the proxy returned in an expression.
不要认为运算符重载有什么特别的,它们就像普通的函数重载一样。两个具有相同名称的函数可以重载,因为它们具有不同的签名。 (顺便说一下,你不能仅通过返回类型来重载 2 个函数。这不仅适用于运算符重载,也适用于一般函数的所有重载。)
就像这样简单:
上面的代码将允许您比较
A
类型的对象与B
类型的对象。它还允许您将A
类型的对象与C
类型的对象进行比较。Don't think of operator overloading as anything special, they are just like normal function overloading. Two functions having the same name can be overloaded given that they have different signatures. (You can't overload 2 functions only by the return type by the way. That is not just for operator overloading, it is for all overloading of functions in general.)
So simply like this:
The above code will allow you to compare an object of type
A
with an object of typeB
. It will also allow you to compare an object of typeA
with an object of typeC
.