Hacker News new | ask | show | jobs
by SamReidHughes 4223 days ago
> Maybe there is a role of operator overloading or templates in C++ but I never really found it.

There is. For templates I'd think the role is obvious, for operator overloading, at least operator-> is indispensable.

1 comments

Many times though, the operator overloading is just a way to hide what's really going on, making it harder for the programmer to have a good grasp of either what the error conditions might be or why there's an expensive operation going on in a particular statement.
Operator overloading is just a function call. Everything you say about overloading an operator could be said about the function call that would replace the overloaded operator. I've never had trouble recognizing that a use of an operator was being done with a non-primitive type, which is the only way the confusion you describe could possibly happen, and the problem you describe has never happened to me while working on C++ code. I don't know why you think it is even plausible.
Operator overloading is a syntactic sugar trick that hides the real method call. Nothing more, nothing less.

It's useful in vector math, complex number math, matrix math.

Operator overloading is useful for a lot more than just math. Overloading * and -> is useful for pointer-like types. Overloading () is useful for function-like types. Overloading [] is useful for collections and overloading ++ and -- is useful for iterators.
The overloading of (), [], and the like is used but it's not necessary. They could have been other named overloaded functions.
A function call is always a function call, an operator is not. It makes for more opaque code.
Function calls aren't always function calls.