Overloading operators

Well, is there ANY concrete use for overloading operators? I mean, is there something that can ONLY be made using overloaded operators? I'm asking this because reading the local tutorial (found on this website), I can't really see much of a use for overloaded operators. If there is any concrete use, can somewone post me an example of code so I can understand it better?
Thanks in advance.
It's for convenience, really. Rather than:
a.add(b)
You can write the nicer looking:
a + b

Operators are really just functions with set parameters, so they can do anything a function can.
closed account (1yR4jE8b)
Pretty much what Zhugge said...

Imagine having a 'number-like' class, and if you need to do some arithmetic using member functions

for example:

((a + b * c )/ d) % e

try to write that using obj.{add,sub, mul, div, mod}() functions in a readable, understandable way and with proper arithmetic precedence.
std::string a, b, c, d, e, f, g;

What's easier to concatenate all of these strings? This:
a.concat ( b.concat ( c.concat ( d.concat ( e.concat ( f.concat ( g ) ) ) ) ) )

Or this:
a+b+c+d+e+f+g

So... basically I'm saying what Zhuge was saying, but I just gave a bit of a better example.
Last edited on
Having operators overloaded would make template functions work better:
1
2
3
4
5
template < class T >
void f ( T a )
{
    // use some operators, using methods would be less generic
}


C++ is meant to work with user-defined types the same way as with basic types
Topic archived. No new replies allowed.