Bitwise Not C++ Operator Overloading Assignment


































































      [49]


























      There has been much discussion over the years about the usefulness of operator overloading in C++ as well as the ability to get it right.

      In reality, it’s hard to get it wrong as long as you follow the canonical forms of the operators and don’t do unexpected things like overloade the operator to perform a subtraction operation. Also, the types that your overloaded operators work with should be consistent.

      The canonical forms of the free function versions of the operators are as follows:

      Canonical forms of the member function versions of operator overloads:

      Note: Assignment operations should always return a reference to the new value (typically, the code would be something like: ).

      There are, of course, notable and somewhat reasonable exceptions to the canonical forms which are valid.

      For example, the “streaming” operator that the C++ standard library uses:

      Or, for value types such as date types, it can make sense to not have the LHS, RHS or return types match for common operations:

      The following operators are explicitly left out of the above because they are either outside of the scope of this discussion, cannot be overloaded, or are dangerous / stupid to overload

        0 comments

        Leave a Reply

        Your email address will not be published. Required fields are marked *