看了STL源码剖析,自己写的:
#include <iostream>
using namespace std;//template<class T>
class Int
{
friend ostream& operator<<(ostream& os,const Int& i);
public:
Int(int i):m_i(i)
{
}
Int& operator++()
{
++(this->m_i);
return *this;
}
Int& operator--()
{
--(this->m_i);
return *this;
}
const Int operator++(int)
{
Int temp=*this;
++(*this);
return temp;
}
const Int operator--(int)
{
Int temp=*this;
--(*this);
return temp;
}
int& operator*() const
{
return (int&)m_i;
}
private:
int m_i;
};
ostream& operator<<(ostream& os,const Int& i)
{
os<<'['<<i.m_i<<']';
return os;
}
int main()
{
Int I(5);
cout<<I++;
cout<<++I;
cout<<I--;
cout<<--I;
cout<<*I;
return 0;
}