If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

C++ Operator: How to deal with operator overloading?

Q: How to deal with operator overloading?

A: Operator overloading looks like a high hill for starters but the key point in their understanding is not considering operator overloading anything other/different from a simple function. This helps a lot especially for beginners!

Consider them normal functions with pre-defined prototypes (for syntactical reasons/benefits while putting them to use so that they just look the way they are used for the fundamental types).

The arithmetic operators work on two operands, for example +, -, /, *, etc. Since there are two things that they work on the function needs to know what those two things(operands) are. The operands are passed to them as arguments to the function. For member functions we show only one argument because of the implicit "this" argument. So, member overloads work on "this" and the second argument commonly named as "rhs" from right-hand-side (of course, you are free to name it differently). As for non-member overloads, there is no "this" involved, hence you give two arguments. The operators would work for your types as the same way they work with the pre-defined types (like say any integral type). They will work for the same way for your class' objects as well. That's it!

That's all there is in operator overloading 101 - the basics. Operator overloading is not something more than mere functions.

Q: How to better overload arithmetic operators?

A: It is recommended to write operator 'op' in terms of their operator 'op='. That is, operator+ in terms of operator+=, operator- in terms of operator-= and similarly operator* and operator/. There are basically two advantages of doing it that way:

You would need to only maintain 'op=' version for your classes.

There could be a gain in performance owing to Return Value Optimization or RVO (see references).

There is no problem with having overloads independently but why ignore the above mentioned advantages? 'operator+=' and 'operator+' can be completely independently defined as well. But don't you think that is a kind of rework/duplicacy (writing same logic twice) when there is an alternative way that may give you an extra bit of performance and also keep your worries away while maintaining the code that you only need to modify 'operator+=' when you need to and leave operator+ untouched?

Another point to note above are two versions of 'operator+'. You only need 1 of those. Even if 'operator+' is or is not defined in terms of 'operator+=' what happens is the object gets "named" and hence all possibilities of "un-named RVO" are lost. There could be gains on certain compilers due to "named RVO". In that case (named RVO), both would be equivalent. And the conclusion that we can derive is that however there (while comparing first argument passed as 'T' or 'const T&') may be no performance boost on such compilers (with both named and unnamed RVO) still you get the advantage related to code maintainance. Doing it this way also makes it unnecessary to make the overloads 'op' as friends.

Q: Why do we need operator overloads as friends?

A: There are basically 2 options - member overloads or non-member overloads. Non-static member functions have this implicit "this" pointer as an argument. Refer to this FAQ - C++ General: What is the 'this' pointer?. This makes functions that require the first argument to be something different that the same object's type to be written as members. For example, consider 'operator<<'. It is usually overloaded for 'ostream' operations. The prototype is similar to this:

For the second definition of 'operator<<', you would not be able to use the above syntax. The primary objective of operator overloading is to keep the semantics as close to their counter-parts for fundamental types as possible. For the second version, we would need to call the operator as follows:

Code:

operator<<(Vector<int>(), std::cout);

which is not we want. Otherwise, what's the use of operator overloading when we can even write a function 'void DisplayVectorContents()' to do that?

Hence, 'operator&lt< cannot be a member - it has to be a non-member for us to be able to specify the first argument as 'std::ostream'. Take a look at the following working sample for illustration:

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.