I understand how to make prefix and postfix incrementers. Within my class DoStuff, I have:
// Prefix; returns the incremented value
friend const DoStuff& operator++(DoStuff& a);
// Postfix: returns the incremented value
friend const DoStuff operator++(DoStuff& a, int);
and outside the class, I have
const DoStuff& operator++(DoStuff& a){
a.num++;
return a;
}
const DoStuff operator++(DoStuff& a, int){
DoStuff before(a.num);
a.num++;
return before;
}
for the prefix and postfix incrementers respectively. What I don't understand is how C++ knows that the former is represented by ++a
and the latter a++
. As far as I can tell, the prefix incrementer references the address &
and that somehow means that the ++
operator symbol should come before it.
Also, I'm not too sure why the postfix needs an int
variable.