class Person {
public:
Person(int age);
public:
Person();
int age;
};
Person::Person(int age) {
this->age = age;
}
Person::Person() {
this->age = 18;//默认18
}
ostream& operator<<(ostream& cout, Person p) {//
cout << p.age;
return cout;
}
//调用时this->operator++可简化为++*this,可以 ++(++*this)
Person& operator++(Person& p) {
p.age = p.age + 1;
return p;
}
//调用时this->operator++(int)可简化为*this++;不可以(*this++)++
Person operator++(Person& p,int) {
Person p1(p);
p.age = p.age + 1;
return p1;
}
void test() {
int a = 12;
Person person(18);
cout << person << endl;
cout << ++(++person) << endl;
cout << person++ << endl;
cout << person;
}
int main() {
test();
}