I’ve been going through Deitel’s C++ Fundamentals and mr. Deitel accented on overloading standard operators to provide standard functionality to custom classes and their members. What I mean is for example instead of cout << object.memberFunction(); I can simply say cout << object;
This technique does allow chaining and quicker typing, but it requires operator overloading implementation and even thou I am still a newb, I fell like the code actually gets less readable, especially if there are many class members you have to remember which ones are operators overloaded for and so on. Without overloaded operators code is much more readable plus you save the overloading code.
So my question is whether I should take the time to learn operator overloading? I am new to C++ and answers from people with more practice and experience is welcome. Will the benefits of operator overloading outweigh the efforts to implement it and the reduced code readability?
I’m tempted to say that there isn’t anything to learn about operator
overloading; overloaded operators are just funny named functions. What
you do have to learn is when overloading is appropriate, and when it is
not. Certain idioms are more or less standard: numeric types overload
the appropriate numeric operators (
+, etc.), smart pointers overloadthe pointer ops (
*and->), container types which support indexationoverload
[], and functional objects overload(). And that’s aboutit for the special cases. And while it’s arguably an abuse, if you
define an iterator, you’ll want it to support the standard iterator
idiom (which means
++,*,->and==/!=).In addition to these, there are three operators which will be overloaded
for many different types of classes: assignment is done using
=, andinsertion into and extraction from streams is done using
<<and>>.If you want your class to support any of these, you should use the
overload. And comparison uses
==and!=for equality, and<,<=,>and>=for inequality. But don’t overload for inequalityunless it is semantically significant. It’s better to provide an
explicit ordering function for use with
std::mapandstd::setthanto mislead readers into thinking you’ve defined as semantically
significant ordering. You might want to specialize
std::lesson yourclass in this case;
<won’t work, or will have inappropriate semanticsfor use as a key, but
std::lesswill define an arbitrary orderingwhich will. And while it’s not operator overloading, if the type is to
be used as a key in associative containers, you’ll also want to provide
a function
hash_codeand an instantiation ofstruct std::hash.In many cases, it’s best to define the overload in terms of some more
global function: for example: I use
compare(returning anintlessthan, equal to, or greater than 0), for inequality; I’ll define a public
function
comparein the class, and then derive from something like:(My implementation is actually somewhat more complex, as it uses
metaprogramming to use
isEqualfor==and!=if the class providesit.)
I use a similar techique for the binary arithmetic operators, defining
+in terms of+=, etc. I also use it for IO, defining<<in termsof
printand>>in terms ofparse; this is mainly useful when theoperators need to be polymorphic.