一个自增与自减的源码

来源:互联网 发布:网上报名软件 编辑:程序博客网 时间:2024/06/11 22:46

看了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;

}



0 0
原创粉丝点击