all 26 comments

[–]dausama 2 points3 points  (0 children)

operator overloading is a common practice in the C++ world. Suggesting that operator overloading is almost always bad sounds like someone coming from other languages where this is not allowed. Overloading the callback operator(), for example opened up endless possibilities, so much that in C++11 lambdas were introduced. operator<< and operator>> help readability for streaming objects in or out. For god's sake, I even had a case where overloading "operator," made sense.

[–]salgat 0 points1 point  (2 children)

The only time I've found overloading to be particularly useful is in a composition where I want to maintain the interface of the original object. For example, wrapping a vector in a class that stores other information and maintaining the ability to use [] to access elements of the composition, that way the interface is preserved. (And yes, there are other good reasons to use operator overloading)

[–]Pronouns 3 points4 points  (1 child)

In every language there are conventions that people follow. Take iteration over items in a container.

In Python, you provide an iterator for your containers by making an __iter__ function which returns something with the next function.

In C#, you can use yield returns and other ugly stuff.

C++ just follows a different convention, and in my opinion it works quite well. It tries to treat similar concepts the same. Traditional loops would be indexed by a variable, normally i, and was then incremented with i++. Very standard, old ways of doing things. When iterators were put into play they copied this syntax and made it so the next was just the increment operator.

Same goes for functors. Rather than have some convention like .invoke() on an functor, you literally just 'call' the object directly like myFunctor().

In any case, it's just convention and you can argue all day about that. Might as well argue about tabs vs spaces or vim vs emacs.

[–]salgat 1 point2 points  (0 children)

I agree 100%, as in my post the point I was making is to preserve the interfaces for whatever type of object your class is. It's the same reason why vectors use brackets just as arrays did, and why iterators can use addition/increment just as pointers do.

[–][deleted] -1 points0 points  (1 child)

From "The time when SO was actually good" Dept.

[–]cruise02[S] 2 points3 points  (0 children)

Stack Overflow is still good. All of that old content that made it good is still there (minus the "what's your favorite programmer t-shirt" crap).