递增运算符重载
#include
class Person
{
public:
void age_set(int age)
{
m_age = age;
}
int age_get()
{
return m_age;
}
/* 前置++ */
Person &operator++()
{
++m_age;
return *this;
}
/* 后置++ */
Person operator++(int)
{
Person p = *this;
m_age++;
return p;
}
private:
int m_age;
};
int main()
{
Person p;
p.age_set(10);
std::cout << (++p).age_get() << std::endl;
std::cout << (p++).age_get() << std::endl;
std::cout << p.age_get() << std::endl;
return 0;
}
$ ./a.out
11
11
12