■ 例子
#include <iostream>
class MyInt{
public:
MyInt(int a):i(a) { }
MyInt& operator++(); // prefix ++
MyInt operator++(int); // postfix ++
friend std::ostream& operator<<(std::ostream&,const MyInt&);
private:
int i;
};
MyInt& MyInt::operator++()
{
this->i++;
return *this;
}
MyInt MyInt::operator++(int)
{
const MyInt temp = *this;
++(*this);
return temp;
}
std::ostream& operator<<(std::ostream& out,const MyInt& t)
{
out << t.i ;
return out;
}
int main()
{
MyInt a(0);
std::cout << a++ << std::endl; // i = 1,print 0
std:: cout << ++a << std::endl; // i = 2,print 2
return 0;
}