Operator overloading seems oddly implemented?

I understand operator overloading (hopefully) but the way it's implemented seems a little awkward. (in my opinion anyways).
Let us say that I have a class called "length" and in it is stored the length of an object in feet and meters (2 separate measurements, not combined). Thus:

1
2
3
4
5
6
7
8
9
class Length
{
private: 
     double meters;
     double feet;
public:
     void getFeet();
     void getMeter();
};


Now let's say that I want to overload the addition operator. So we would add these function to the class declaration:

1
2
Length operator+(Length)
friend Length operator+(double, Length)


Alright, cool. Got some overloaded pluses going on. But what if I wanted to do a length plus a double. Because I want the overloaded addition operator to act similar to the natural addition operator I would want to be able to add terms in any order I desire. Like length + double, double + length, and length + length. In the end I'd have 3 different operator overloading functions just for 1 arithmetic operator. Now let's say I want to do the addition, minus, multiplication, and division operators. All of them would need at least 3 different functions, so that's 12 total. Just for the 4 basic arithmetic operators. I realize that a lot of the code can be reused, like for the addition example you could do this:

1
2
// Reusing code, handy indeed
friend Length operator+(Length, double) { return (double + Length);}


and you can also use the +=, -=, etc to farther simplify these. But this still seems...excessive. Do most people normally just allow one route? Do you not worry about enabling all of the combinations? Is it uncommon (or bad practice) to use/overload so many different operators for one class? Thanks for any help.
Last edited on
As you said, you could overload '+=' and implement the others using that. They will be have the same structure for any class, so you could templatize those wrappers.

That approach is used to overload '>', '<=', '>=' having only '<' defined in your class http://cplusplus.com/reference/std/utility/rel_ops/

By the way, Length operator+(double, Length) does not make sense
Thanks for the help, I'll read through that and hopefully it'll explain it a little better than the other things that I've read. And also in the part that doesn't make sense I meant to make that friend, wasn't paying enough attention unfortunately. Fixed in original post now though.
Topic archived. No new replies allowed.