In the last section we breifly mentioned overloading unary operators, such as ++ and --. It really isn't too different from overloading a binary operator such as +. Here's a quick example which will incremenet the data in our burrito variable by one and return the new burrito. class Burrito { private: int amtbeef, amtbean; public: Burrito(int newbeef, newbean) { amtbeef = newbeef; amtbean = newbean; } Burrito operator ++() { amtbeef++; amtbean++; return Burrito(amtbeef, amtbean); } }; In the above example, we see many similarities to our previous example of a binary overloaded operator. In fact the only real difference is that there is no parameter passed to the operator method, which makes sense since we are only concerned with one operand, which is the object whose operator is being used.
However, there is one small caveat that you must keep in mind specifically when
overloading the ++ and -- operators. As you saw earlier, the statement
|