0
How to call overloaded operator from different overloaded operator
Hi I have overloaded < operator as well as > operator for a custom class. I would like to remove one function definition and Instead call other one with negate effect. For example Call < overloaded operator and return reverse of result as output of > operator. https://sololearn.com/compiler-playground/cE5smx6LGeKH/?ref=app
6 Réponses
+ 1
The link you have shared is broken and I cannot open it from Windows OS laptop.
+ 1
I can check link in android app.
No, question is not same.
Here, I am ok with Two definition but in a different way
Something like below:
bool islessthan(int a,int b)
{
return a<b;
}
bool isgreateethan(int a,int b)
{
return !islessthan(a,b);
}
Basically, I need to call operator < from operator >
+ 1
I understand your point of inequality and < with > but I need a mechanism if available to call one overloaded operator from another overloaded operator
0
is this a duplicate post?
https://www.sololearn.com/Discuss/3311030/?ref=app
0
maybe. But wouldn't your isgreaterthan be actually be >= ?
!(a<b) also includes a==b
it might or not matter, depending on your use case, like sorting, but > would be wrong if you were to use it for two equal values...
it would be better to use
bool isgreaterthan(int a, int b){
return islessthan(b, a);
}
0
relational operators can be constructed from at minimum of 2 operations. Since < is required by the stl, the other would be ==
define these:
a < b a less than b
a == b a equal to b
from those two we can derive:
b < a a greater than b a > b
!(a == b) a not equal to b a != b
!(a < b) a greater than or equal to b a >= b
!(b > a) a less than or equal to b a <= b
https://www.geeksforgeeks.org/relational-operator-overloading-in-cpp/