C++ Post increment and pre increment

Tags:

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.14

C++ FAQ suggests the following for post increment.

 class Number {
 public:
   Number& operator++ ();    // prefix ++
   Number  operator++ (int); // postfix ++
 }; 

But actually is should be

const Number operator++(int);

to prevent num++++ which semantically doesn’t make any sense.

Similarily, operator+ should return const Number.

Comments

Leave a Reply

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