如何在重载运算符中引用当前结构?
我有一个结构体,我想通过定义 << 来定义相对顺序。 ,> 、<= 和 >= 运算符。实际上,按照我的顺序,不会有任何相等,因此如果一个结构不小于另一个结构,它会自动更大。
我像这样定义了第一个运算符:
struct MyStruct{
...
...
bool operator < (const MyStruct &b) const {return (somefancycomputation);}
};
现在我想基于这个运算符定义其他运算符,这样 <= 将返回与 <= 相同的结果。另外两个只会返回相反的结果。 例如对于 >我想写一些类似的东西
bool operator > (const MyStruct &b) const {return !(self<b);}
,但我不知道如何引用这个“self”,因为我只能引用当前结构内的字段。
整个都是用 C++ 写的,
希望我的问题可以理解:)
谢谢你的帮助!
I have a struct for which i want to define a relative order by defining < , > , <= and >= operators. actually in my order there won't be any equality, so if one struct is not smaller than another, it's automatically larger.
I defined the first operator like this:
struct MyStruct{
...
...
bool operator < (const MyStruct &b) const {return (somefancycomputation);}
};
now i'd like to define the other operators based on this operator, such that <= will return the same as < and the other two will simply return the oposite.
so for example for the > operator i'd like to write something like
bool operator > (const MyStruct &b) const {return !(self<b);}
but i don't know how to refere to this 'self' since i can refere only to the fields inside the current struct.
whole is in C++
hope my question was understandable :)
thank you for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
自我是
*this
。也就是说,
this
是一个指向当前对象的指针,因此您需要取消引用它才能获取实际的对象。Self is
*this
.That is,
this
is a pointer to the current object, so you need to dereference it to get the actual object.如果您提供具有所有适当逻辑的
运算符<
,那么(无论它是否作为自由函数实现)您可以将其他运算符作为自由函数实现。这遵循在可能的情况下优先选择非成员而不是成员的规则,并且自由函数在左右操作数的转换方面将具有相同的行为,而作为成员函数实现的运算符则不然。例如
If you are providing an
operator<
with all the appropriate logic then (whether or not it is implemented as a free function) you can implement the other operators in terms of it as free functions. This follows the rule of preferring non-members over members where possible and free functions will have identical behaviour w.r.t. conversions of the left and right operands, whereas operators implemented as member functions don't.e.g.
这意味着对 元素的答案:
This is meant as a slight improvement over Element's answer:
重载运算符只是一个成员函数,尽管语法很奇特。
您可以像这样显式调用它:
或者为自己节省一些头发:),只需提供 intcompare( const MyStruct& ) 成员函数并在所有非成员比较运算符中使用它。
Overloaded operator is just a member function though with a fancy syntax.
You can explicitly call it like this:
or save yourself some hair :) and just provide
int compare( const MyStruct& )
member function and use that in all the non-member comparison operators.这是指向当前对象的指针。所以(正如 @Dave Hinton 所说)你必须取消引用它。但是,还有另一种选择
两种选择:
是的,第一个更好。
this is a pointer to your current object. So (as @Dave Hinton says) you'd have to dereference it. But, theres another option
Two options:
Yes, the first one is quite nicer.
这是一个问题。 C++ 操作(以及许多算法)需要严格的弱排序,其中(其他)意味着
x == x
对于任何x
都成立。更一般地说:暗示
任何
x
和y
。换句话说:您很可能必须为您的结构定义某种等式才能与任何传统算法一起使用。That's a problem. C++ operations (and many algorithms) require a strict weak ordering, which (among others) implies that
x == x
holds for anyx
. More generally:implies
for any
x
andy
. In other words: you most probably must define some kind of equality for your struct to work with any conventional algorithm.我同意上面的 Tom/Dave 的直接写法:
但对我来说,最明确清晰的形式是:
和 Tom 的: return !(this->operator<(b));似乎有点傻。
这个有点讨厌的 C 预处理器代码(我还有一个更好但更具技术性的模板解决方案):
自动定义所有比较运算符,假设 == 和 >定义如下:
I'm with Tom/Dave above in that the direct way to write this:
but for me the most explicitly clear form is:
and Tom's: return !(this->operator<(b)); seems a little silly.
This little bit nasty C preprocessor code (I also have a better but more technical template solution):
automatically defines all comparison operators assuming == and > are defined like so: