标签:c++
class Int { friend ostream& operator<<(ostream& os, const Int& i); friend istream& operator>>(istream& is, Int& i); friend bool operator<(const Int& a, const Int& b); private: int value; public: Int():value(0){} Int(int val):value(val){} Int(const Int& i):value(i.value){} Int& operator=(const Int& i) { value = i.value; return *this; } //前置操作符重载 Int& operator++() { ++value; return *this; } Int& operator--() { --value; return *this; } //后置操作符重载 Int operator++(int) { Int ret(*this); ++value; return ret; } Int operator--(int) { Int ret(*this); --value; return ret; } }; ostream& operator<<(ostream& os, const Int& i) { os << i.value; return os; } istream& operator>>(istream& is, Int& i) { is >> i.value; return is; } bool operator<(const Int& a, const Int& b) { return a.value < b.value; } int main() { /* vector<Int> v; copy(istream_iterator<Int>(cin), istream_iterator<Int>(), back_inserter(v)); sort(v.begin(), v.end()); copy(v.begin(), v.end(), ostream_iterator<Int>(cout, " ")); cout << endl; */ Int v = 10; cout << ++v << endl; cout << v++ << endl; cout << v << endl; return 0; }
标签:c++
原文地址:http://blog.csdn.net/yusiguyuan/article/details/38521175