取值(dereference,operator*),前置式和后置式自增.
#include <iostream>
using namespace std;
class INT
{
public:
INT(int m):m_i(m){}
friend ostream& operator<<(ostream& os,const INT& i);
//prefix:increment and then fetch
INT& operator++()
{
++(this->m_i);
return *this;
}
//postfix:fetch and then increment
const INT operator++(int)
{
INT temp = *this;
++(*this);
return temp;
}
//prefix:decrement and fetch
INT& operator--()
{
--(this->m_i);
return *this;
}
//postfix:fetch and then decrement
const INT operator--(int)
{
INT temp =*this;
--(*this);
return temp;
}
//dereference
int& operator*()
{
return (int&)m_i;
}
private:
int m_i;
};
ostream& operator<<(ostream& os,const INT& i)
{
os<<i.m_i<<endl;
return os;
}
int main()
{
INT i(5);
cout<<++i;
cout<<i++;
cout<<--i;
cout<<i--;
cout<<*i;
return 0;
}