What is the need for operator overloading?

Sometimes we create complex objects that need special handling when we do things like add them together (or subtract).

http://en.wikipedia.org/wiki/Operator_overloading

Let's say you have a class that has a description and a percentage.
You could add object+object+object (etc.) but you might want to ensure the value never goes above 100%. Overloading the + operator would allow you to ensure that.

Let's also say you want to determine what a comparison of Equals means.
Comparing object==object might not return what you expect until you specifically tell the compiler HOW to compare (or which elements are compared) when asking ==.

In other words, simplistically, it's a way to define what ==, <, >, etc are meant to be for a class that you define yourself.

Example, you have a class of CAR which contains
Make
Model
Year
Price

You can define operator= to mean:
Same Make
Same Year
Price is within $1500 of each other.

Of course at thines01 said, you can also define how to ADD and SUBtract two cars -- whatever you think that would mean...

Edited 4 Years Ago by WaltP: n/a

But we can do these without overloading also. Java do not support operator overloading so why to do in c++ and c#? I am not getting the actual usage.

Overloading an operator is overhead.
Means in totality there is no actual use.

But we can do these without overloading also.

Sure. We can make a function: CAR AddTwoCars(CAR vehicle1, CAR vehicle2, CAR vehicleNew) which 'adds' #1 & #2 and returns the new one. Or we can overload the + operator and have: vehicleNew = vehicle1 + vehicle2;

Java do not support operator overloading so why to do in c++ and c#? I am not getting the actual usage.

So if Java doesn't do something, C++ and C# shouldn't either?
Well, ForTran doesn't have classes and pointers. Why should Java, C, C++, C#, Python, ...?

Overloading an operator is overhead.

Only as much as any other function, not really a problem in general

Means in totality there is no actual use.

I'd disagree with this. You are correct in the sense that, since and operator is basically a function, it would always be possible to do whatever it is that you want to do using a "traditional" functions. However, in lots of cases, an overloaded operator is much more readable than a regular function. And, readability should NEVER be underestimated. The other area where operator overloading is very useful, is if you introduce a new class/concept and you want to communicate that certain behaviours are equivalent to some existing behaviour. The best example I can think of this is STL iterators. Iterators overload many operators to make them behave like pointers in situations where they will basically be used like pointers, for example: for ( it = it_start; it != it_finish; ++it ) or it->MyMemberFunction(); or SomeClass x = *it; All of these could be done using specific iterator member functions (since that's all an operator overload is), but I prefer for ( it = it_start; it != it_finish; ++it ) to for ( it.set_position( it_start ); it.not_equal_to( it_finish ); it.increment() ) .

@WaltP:
i am not saying that if java does not support than c# should not. I mean to say is java has eliminated the concept of operator overloading and than too we don't need any operator overloading there. So, why we use it here? Well, i think operator overloading is required when we want to do some operation by creating more than one class objects so that it becomes easy to pass data to function. An i right?

@WaltP:
i am not saying that if java does not support than c# should not. I mean to say is java has eliminated the concept of operator overloading and than too we don't need any operator overloading there.

Nitpick: When did Java eliminate overloading? What version of Java took it out of the definition?

Be careful with your wording...

So, why we use it here?

Simplistic answer: Because when C++ was defined overloading was a logical enhancement to the object concept. Why have objects if we can't define operators to operate on them?

Well, i think operator overloading is required when we want to do some operation by creating more than one class objects so that it becomes easy to pass data to function. An i right?

Not a clue what you mean.

In Java we don't have operator overloading. We have function overloading

There is no law saying you MUST use this concept.
You also can write code without loops and without user-defined functions.

Some languages don't have conventions for direct memory writes or pointers or multiple inheritance or macros or header files or deterministic destruction.

Some languages have implicit types (Duck Typing).
Some languages come with interactive consoles.
Some languages don't require you to compile and link.

Yes i also understand that
loops are for convienance. That make work easy, while this is increasing burden.

If at some point, you're going to perform that operation (combining, subtracting, comparing) ANYWAY, you'll still be writing the same code.

But, if you don't need it, don't use it.

This article has been dead for over six months. Start a new discussion instead.