C++ Post increment and pre increment

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.

Similar Posts:

Post a Comment

Your email is never published nor shared.