Overloading ++ & -- operators

There are two version of the increment and decrement operators, those being prefix and postfix:

 

  • increment
    • prefix: increment the value, then use it
      • ++a
    • postfix: use the value, then increment it
      • a++
  • decrement
    • prefix: increment the value, then use it
      • --a
    • postfix: use the value, then increment it
      • a--

Since there are two types of the same operator (pre/post fix) C++ introduced a hack to enable the compiler to determine which version is required.

 

The prefix versions take no parameters.

 

The postfix versions take a dummy int that is not used.

 

Their prototypes are typically as follows:

  • MyClass & operator++() ;  //prefix
  • const MyClass & operator++(int) ;  //postfix
  • MyClass & operator--() ;  //prefix
  • const MyClass & operator--(int) ;  //postfix

 

We can tell the difference between the prefix and the postfix, since the postfix operators have a dummy int in their signatures. Since the dummy is not  used or given a name, the compiler knows to treat this as a placeholder and therefore not bother with warnings that it has been declared but not used.

 

Both forms of the methods perform the same functionality and we can see that the postfix versions actually call the prefix versions to carry out the actual increment/decrement, but the only difference is in the return values. Specifically, the postfix versions return the original value that has been temporarily stored whilst the prefix inc/dec method is called to actually carry out the inc/dec and the original value is then returned, thus performing a postfix inc/dec.

 

In the example below, lines 3 and 7 of the output show that the prefix versions of the overloaded operators are called:

#include <iostream>
using namespace std;

class MyClass {
	private:
		int myVar ;

	public:
		MyClass(int val = 0) : myVar(val) {} //constructor

		MyClass & operator++() ;  //prefix
		MyClass & operator--() ;  //prefix

		const MyClass & operator++(int) ;  //postfix
		const MyClass & operator--(int) ;  //postfix

		int getVal(){return myVar;}
} ;

MyClass& MyClass::operator++() {  //prefix
	cout << "increment before PREfix " << myVar ;
		++myVar ;
	cout << ", increment after PREfix " << myVar << endl ;
		return *this ;
}
MyClass & MyClass::operator--() {  //prefix
	cout << "decrement before PREfix " << myVar ;
		--myVar ;
	cout << ", decrement after PREfix " << myVar << endl ;
		return *this ;
}
const MyClass & MyClass::operator++(int) {  //postfix
	cout << "increment before POSTfix " << myVar << endl ;
	MyClass temp(myVar) ;
	++*this;  //call the prefix version
	cout << "increment after POSTfix " << myVar << endl ;
	return temp;
}
const MyClass & MyClass::operator--(int) {  //postfix
	cout << "increment before POSTfix " << myVar << endl ;
	MyClass temp(myVar) ;
	--*this;  //call the prefix version
	cout << "increment after POSTfix " << myVar << endl ;
	return temp;
}

int main() {

	MyClass a(6);

	++a;
	a++;
	--a;
	a--;

	return 0 ;
}

Compile & Run:

increment before PREfix 6, increment after PREfix 7
increment before POSTfix 7
increment before PREfix 7, increment after PREfix 8
increment after POSTfix 8
decrement before PREfix 8, decrement after PREfix 7
increment before POSTfix 7
decrement before PREfix 7, decrement after PREfix 6
increment after POSTfix 6

Leave a Reply