9.5 — Overloading unary operators +, -, and !

Overloading unary operators

Unlike the operators you’ve seen so far, the positive (+), negative (-) and logical not (!) operators all are unary operators, which means they only operate on one operand. Because they only operate on the object they are applied to, typically unary operator overloads are implemented as member functions. All three operands are implemented in an identical manner.

Let’s take a look at how we’d implement operator- on the Cents class we used in a previous example:

This should be straightforward. Our overloaded negative operator (-) is a unary operator implemented as a member function, so it takes no parameters (it operates on the *this object). It returns a Cents object that is the negation of the original Cents value. Because operator- does not modify the Cents object, we can (and should) make it a const function (so it can be called on const Cents objects).

Note that there’s no confusion between the negative operator- and the minus operator- since they have a different number of parameters.

Here’s another example. The ! operator is the logical negation operator -- if an expression evaluates to “true”, operator! will return false, and vice-versa. We commonly see this applied to boolean variables to test whether they are true or not:

For integers, 0 evaluates to false, and anything else to true, so operator! as applied to integers will return true for an integer value of 0 and false otherwise.

Extending the concept, we can say that operator! should evaluate to true if the state of the object is “false”, “zero”, or whatever the default initialization state is.

The following example shows an overload of both operator- and operator! for a user-defined Point class:

The overloaded operator! for this class returns the boolean value “true” if the Point is set to the default value at coordinate (0.0, 0.0, 0.0). Thus, the above code produces the result:

point is set at the origin.

Quiz time

1) Implement overloaded operator+ for the Point class.

Show Solution

9.6 -- Overloading the comparison operators
9.4 -- Overloading operators using member functions

95 comments to 9.5 — Overloading unary operators +, -, and !

  • sv

    i did this and i thought output would be ---   "A nickle of debt is worth    hello   -5 cents"
    but the actual output is ---- "    hello    A nickle of debt is worth -5 cents"

    can someone explain why did this happen?
    whats the flow of control here?

    • nascardriver

      It's undefined, the functions can be called in any order. If you need functions to be called in a specific order, call them one after the other.

  • Nguyen

    "Extending the concept, we can say that operator! should evaluate to true if the state of the object is “false”, “zero”, or whatever the default initialization state is."

    What is the state of the object in the last example?  I can't relate the state of the object to the example.
    What I see is the condition (!point) in the if statement becomes true or false depending on the boolean value of true or false returned from the overloaded operator!

    • nascardriver

      "state" is the same as "values" in this case. The default initialization state is when all members have their default value (0).

  • David

    Hello,I have a question in your example. Before we call the operator- , the value of nickle is 5. However,when we call the operator-(),the value of nickle becomes -5. And why can you say the operator-() doesn't modify the object?
    In my opinion , its behavior violated the const meaning.
    Does my concept be wrong?

    • nascardriver

      The value of `nickle` doesn't change. `operator-` creates a new `Cents` object with value -5. You can print `nickle.getCents()` in line 27 to verify this.

  • Ged

    Is there any difference if this function is outside of the class or inside?

  • hellmet

    I was wondering why not have the operator- () function be left in the class itself?

  • Parsa

    what about returning just by reference? Not const reference.
    Is the object returned an r-value? So you can't return it by reference?

Leave a Comment

Put all code inside code tags: [code]your code here[/code]