- #1
chingkui
- 181
- 2
I have seen how people implement the prefix and postfix ++ overloading, which are as follow:
Number& operator++ () // prefix ++
{
// Do work on this.
return *this;
}
Number operator++ (int) // postfix ++
{
Number result(*this); // make a copy for result
++(*this); // Now use the prefix version to do the work
return result; // return the copy (the old) value.
}
What I don't understand is:
When you call ++m1 and m2++, how does the machine know it should call Number& operator++ () for m1 and Number operator++ (int) for m2? I look at it and think about it for a long time, but I still couldn't tell from the syntax how it is done. Did I miss something obvious?
Number& operator++ () // prefix ++
{
// Do work on this.
return *this;
}
Number operator++ (int) // postfix ++
{
Number result(*this); // make a copy for result
++(*this); // Now use the prefix version to do the work
return result; // return the copy (the old) value.
}
What I don't understand is:
When you call ++m1 and m2++, how does the machine know it should call Number& operator++ () for m1 and Number operator++ (int) for m2? I look at it and think about it for a long time, but I still couldn't tell from the syntax how it is done. Did I miss something obvious?