//4.5.3 递增运算符重载 //作用:通过重载递增运算符,实现自己的整形数据 #if(0) #include class MyInteger { friend std::ostream& operator<<(std::ostream& cout, MyInteger myint); public: MyInteger() { m_Num = 0; } //重载前置++运算符 返回引用为了一直对一个数据进行递增操作 MyInteger& operator++() { //先进行++运算 m_Num++; //再将自身做返回 return *this; } //重载后置++运算符 //void operator++(int) int代表的是一个占位参数 可以用于区分前置和后置递增 MyInteger operator++(int) { //先 记录当时结果 MyInteger temp = *this; //后 递增 m_Num++; //最后将记录结果做返回 return temp; } private: int m_Num; }; //重载<<运算符 std::ostream& operator<<(std::ostream&cout , MyInteger myint) { std::cout << myint.m_Num; return cout; } void test01() { MyInteger myint; std::cout << ++(++myint) << std::endl; std::cout << myint << std::endl; } void test02() { MyInteger myint; std::cout << myint++ << std::endl; std::cout << myint << std::endl; } int main() { //test01(); test02(); //int a = 0; //std::cout << ++(++a) << std::endl; //std::cout << a << std::endl; system("pause"); return 0; } #endif //总结 :前置递增返回的是引用,后置递增返回的是值