When overloading the operator ++, ________ is used to distinguish preincrement from postincrement.

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Increment and Decrement Operator Overloading (C++)

  • Article
  • 08/10/2021
  • 2 minutes to read

In this article

The increment and decrement operators fall into a special category because there are two variants of each:

  • Preincrement and postincrement

  • Predecrement and postdecrement

When you write overloaded operator functions, it can be useful to implement separate versions for the prefix and postfix versions of these operators. To distinguish between the two, the following rule is observed: The prefix form of the operator is declared exactly the same way as any other unary operator; the postfix form accepts an extra argument of type int.

Note

When specifying an overloaded operator for the postfix form of the increment or decrement operator, the additional argument must be of type int; specifying any other type generates an error.

The following example shows how to define prefix and postfix increment and decrement operators for the Point class:

// increment_and_decrement1.cpp
class Point
{
public:
   // Declare prefix and postfix increment operators.
   Point& operator++();       // Prefix increment operator.
   Point operator++(int);     // Postfix increment operator.

   // Declare prefix and postfix decrement operators.
   Point& operator--();       // Prefix decrement operator.
   Point operator--(int);     // Postfix decrement operator.

   // Define default constructor.
   Point() { _x = _y = 0; }

   // Define accessor functions.
   int x() { return _x; }
   int y() { return _y; }
private:
   int _x, _y;
};

// Define prefix increment operator.
Point& Point::operator++()
{
   _x++;
   _y++;
   return *this;
}

// Define postfix increment operator.
Point Point::operator++(int)
{
   Point temp = *this;
   ++*this;
   return temp;
}

// Define prefix decrement operator.
Point& Point::operator--()
{
   _x--;
   _y--;
   return *this;
}

// Define postfix decrement operator.
Point Point::operator--(int)
{
   Point temp = *this;
   --*this;
   return temp;
}

int main()
{
}

The same operators can be defined in file scope (globally) using the following function prototypes:

friend Point& operator++( Point& );      // Prefix increment
friend Point operator++( Point&, int );  // Postfix increment
friend Point& operator--( Point& );      // Prefix decrement
friend Point operator--( Point&, int );  // Postfix decrement

The argument of type int that denotes the postfix form of the increment or decrement operator isn't commonly used to pass arguments. It usually contains the value 0. However, it can be used as follows:

// increment_and_decrement2.cpp
class Int
{
public:
    Int operator++( int n ); // Postfix increment operator
private:
    int _i;
};

Int Int::operator++( int n )
{
    Int result = *this;
    if( n != 0 )    // Handle case where an argument is passed.
        _i += n;
    else
        _i++;       // Handle case where no argument is passed.
    return result;
}

int main()
{
   Int i;
   i.operator++( 25 ); // Increment by 25.
}

There's no syntax for using the increment or decrement operators to pass these values other than explicit invocation, as shown in the preceding code. A more straightforward way to implement this functionality is to overload the addition/assignment operator (+=).

See also

Operator Overloading

Feedback

Submit and view feedback for

Q: Which of the following is false about the new operator and the object for which it allocates memory?

Get answer to your question and much more

Q: Which of the following is false?

Get answer to your question and much more

Q: The delete [] operator:

Get answer to your question and much more

Q: The conventional way to distinguish between the overloaded preincrement and postincrementoperators (++) is:

Get answer to your question and much more

Q: The correct function name for overloading the addition (+) operator is:

Get answer to your question and much more

Q: An explicit constructor:

Get answer to your question and much more

Q: An overloaded + operator takes a class object and a double as operands. For it to be commutative(i.e., a + band b + a both work):

Get answer to your question and much more

Q: Which statement about operator overloading is false?

Get answer to your question and much more

Q: Assume that the function call operator() is overloaded for data type String in the usual sense ofselecting a substring from a larger string. For a String object string1 with the character string"ABCDEFGHI", what string does string1( 4 , 2 ) return?

Why (+) operator is also called an overloaded operator?

Operator Overloading in C++ This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.

What is the use of operator overloading?

Operator overloading facilitates the specification of user-defined implementation for operations wherein one or both operands are of user-defined class or structure type. This helps user-defined types to behave much like the fundamental primitive data types.

How do you overload the ++ operator in C++?

The postfix increment operator ++ can be overloaded for a class type by declaring a nonmember function operator operator++() with two arguments, the first having class type and the second having type int . Alternatively, you can declare a member function operator operator++() with one argument having type int .

When you overload the operator you must also overload the operator?

When you overload the << operator, you must also overload the >> operator. A static member variable can be used when there are no objects of the class in existence. A public data member may be declared a friend of a private function.