Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

C++ Short-Circuit Operators_Increment and Decrement

Example 5-24. Overloading the increment operator
enum status { stopped, running, waiting };
status& operator++(status& s) { // Prefix
 if (s != waiting)
 s = status(s + 1);
 return s;
}
status operator++(status& s, int) { // Postfix
 status rtn = s;
 ++s;
 return rtn;
}
int main( )
{
 status s(stopped);
 ++s; // Calls operator++(s);
 s++; // Calls operator++(s, 0);
}
 
PREVIOUS NEXT
Tagged: #Decrement
ADD COMMENT
Topic
Name
8+4 =