标准库中iterator对++/--的重载代码如下:
1 _Myiter& operator++() 2 { // preincrement 3 ++*(_Mybase *)this; 4 return (*this); 5 } 6 7 _Myiter operator++(int) 8 { // postincrement 9 _Myiter _Tmp = *this; 10 ++*this; 11 return (_Tmp); 12 } 13 14 _Myiter& operator--() 15 { // predecrement 16 --*(_Mybase *)this; 17 return (*this); 18 } 19 20 _Myiter operator--(int) 21 { // postdecrement 22 _Myiter _Tmp = *this; 23 --*this; 24 return (_Tmp); 25 }
C++:重载前置++/--返回引用,重载后置++/--返回临时对象
原文:https://www.cnblogs.com/XiaoXiaoShuai-/p/11628495.html