3 ms·
> 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 op
by SamReidHughes 12y 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.
- Nursie 12y agoMany 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.
- SamReidHughes 12y agoOperator 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.
- Nursie 12y agoA function call is always a function call, an operator is not. It makes for more opaque code.
- SamReidHughes 12y agoFunction calls aren't always function calls.
- vardump 12y agoOperator 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.
- TheCoelacanth 12y agoOperator 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.
- SamReidHughes 12y agoThe overloading of (), [], and the like is used but it's not necessary. They could have been other named overloaded functions.