++ overloading

CmplxNo operator++ (int); //this is postfix ++
CmplxNo& operator++ (); //this is a prefix++

Q1. why is int in postfix
Q2. why postfix has return type as a by value while prefix has reference return type ??
Last edited on
Also,
While writing the definition for these declaration,
how would i use the int. could someone write a code and explain this plz.

for prefix
CmplxNo& CmplxNo::operator++(void)
{
CmplxNo.re += 1;
CmplxNo.im += 1;
return *this;
}

what will be for postfilx
For postfix, you will have to create a temporary variable to hold the state before you have incremented and return that when you are finished. The int you have to pass is a dummy variable and is only used to differentiate the postfix and the prefix operators.
but why the different
output value types for each of them. could prefix also not have only cmplxNo as output rather than cmplxNo& ??
Because the postfix is returning a temporary variable you return from the function, which is destroyed when you close the function. So you take a copy of it instead of a reference.

Prefix can return a reference since the object still exists after the completion of the function, and the user may want to perform more operations on that object within the same expression.
ya . i get it.

thx a lot zhuge !

~Cheers!
Neeraj
Topic archived. No new replies allowed.