|
| |||||||||||||||||||||||||||||||||||||||||||||
| kibestar (42) | |
|
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. | |
|
|
|
| Zhuge (2205) | |
It's for convenience, really. Rather than:a.add(b)You can write the nicer looking: a + bOperators are really just functions with set parameters, so they can do anything a function can. | |
|
|
|
| darkestfright (800) | |
|
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) % etry to write that using obj.{add,sub, mul, div, mod}() functions in a readable, understandable way and with proper arithmetic precedence. | |
|
|
|
| NGen (440) | |
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+gSo... basically I'm saying what Zhuge was saying, but I just gave a bit of a better example. | |
|
Last edited on
|
|
| Bazzy (6258) | |||
Having operators overloaded would make template functions work better:
C++ is meant to work with user-defined types the same way as with basic types | |||
|
|
|||