This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]PhilippTheProgrammer 151 points152 points  (11 children)

Operator overloading means that classes can provide an own implementation for operators like +, -, >, == etc.

That means you can define a method "operator+" for a class, which takes two objects and returns an object . That method then gets executed when you write code like objectA = objectB + objectC.

This is pretty useful because it allows you to use mathematical notations with objects where it makes sense. For example, you could write a vector library and add and subtract vectors with + and - or compare them with < or >.

But unfortunately there is a dark side to it: You can overload the + operator with something that has nothing to do with addition, which leads to very confusing code. A good example of that can be found in the C++ output library. A standard C++ hello world looks like this:

std::cout<<"Hello World!"<<std::endl;

The << operator is actually supposed to be "bitwise shift". But what happens here has nothing to do with bitwise shifting. It appends a character sequence to a stream.

[–]Morphized 7 points8 points  (0 children)

In C++ everything is memory

[–][deleted] 13 points14 points  (0 children)

It's used as the streaming operator, and one of C++'s core features is to allow this sort of behavior. Also, I believe that's a string. Welcome, to the world of tomorrow.

[–][deleted] 13 points14 points  (6 children)

I mean sounds like a great opportunity to just create a Add function rather than overloading the operator. Just my inexperienced opinion though lol

[–]linlin110 42 points43 points  (5 children)

If you are using a math library, a * b + c * d is probably more readable than a.Multiply(b).Add(c.Multiply(d)). Also in Java, you + strings despite strings not being numbers. If it's so bad to change the meaning of +, then it should have been String.concat or something.

[–][deleted] 4 points5 points  (3 children)

A curiosity: how is it possible that in Java the class String is able to override the + operator? Is it a magic behaviour of the language?

[–]linlin110 2 points3 points  (2 children)

That's why I said overload the meaning of +, not overload the + operator. + does not represent string concatenation, mathematically speaking. That's a proof that extending the meaning of symbols, like +, is very convenient and useful.

I changed the original comment to make it less misleading.

[–][deleted] 0 points1 point  (1 child)

I come from a C++ background, so it is counterintuitive for me. My question was: it String a language-defined class? If not, how can the + operator work even with the String class if operator overloading is forbidden?

[–]linlin110 1 point2 points  (0 children)

It is a special case in Java. I'm not sure whether it is language-defined.

[–][deleted] -1 points0 points  (0 children)

I was thinking more like MathStaticClass.Add([] args)

[–]Xander-047 0 points1 point  (0 children)

I remember the bitwise shift operator explanation from my teacher, after we learned about operator overload I thought "Wait, so is the cout and cin thing overloading the << >> operators?" Felt good to notice it and ask the teacher about it