Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to refer to the current struct in an overloaded operator?

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!


1 Answers

Self is *this.

That is, this is a pointer to the current object, so you need to dereference it to get the actual object.

like image 83
dave4420 Avatar answered Dec 20 '25 15:12

dave4420



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!