954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need of OperatorOver loading

What is the need for operator overloading?

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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 ==.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Convenience is one benefit.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
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, ...?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
Overloading an operator is overhead.


Only as much as any other function, not really a problem in generalMeans 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 morereadable 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() ) .

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

@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?

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
@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 theobject 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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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

Jigs28
Light Poster
42 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You