i did an overloading of the + operator but now i wanna do overloading of == operator of 2 lengths (may or may not be the same length) and return the respective results. How do i do it? Do i need to use bool for ==?
//what i did for overloading + operator to get new length out of 2 different lengths
Length operator+ (const Length& lengthA){
int newlengthMin = min, newlengthMax = max;
if (lengthA.min < min)
newLengthMin = lengthA.min;
if (lengthA.max > max)
newLengthMax = lengthA.max;
return Length(newLengthMin, newLengthMax);
}
For the simple case, use bool operator==(const Length& other) const
. Note the const
- a comparison operator shouldn't need to modify its operands. Neither should your operator+
!
If you want to take advantage of implicit conversions on both sides, declare the comparison operator at global scope:
bool operator==(const Length& a, const Length& b) {...}
Use bool and make sure to add const
as well.
bool operator==(const Length& lengthA) const { return ...; }
You can also make it global, with two arguments (one for each object).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With