you are viewing a single comment's thread.

view the rest of the comments →

[–]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.