#include <iostream>
using namespace std;
class person {
public:
int age;
person(int v = 0) : age(v) {}
friend const person & operator++(person &); //pref
friend const person operator++(person &, int); //post
};
const person& operator++(person& a) {
a.age++;
return a;
}
const person operator++(person& a, int) {
person aux = a;
a.age++;
return aux;
}
int main() {
person p1(25), p2;
p2 = ++p1;
cout << p2.age<<endl;
p2 = p1++;
cout<< p2.age<<endl;
cout << p1.age;
}
Maybe the person who wrote this piece used some 'extra slang' that isn't necessary or maybe I haven't read the manual enough but I don't understand the following:
What's the point of 'const' in here and what does 'person&' mean? The person &a or person& a in the () I think it means it gets passed by reference. Is it correct?
friend const person& operator++(person &a);
Why here person doesn't have an & after it like above? I understand that the arguments are a reference and an int. But what int? I don't see it anywhere that function beign called like that, p1(25) in the int main() it's the constructor not a function. How does he know what to do when p1++ is invoked and when ++p is invoked. I know about overloading I just don't get it how that & after person in the first function works and what is the int in the second one. Thank you.
const person operator++(person& a, int)