1)i++ 返回的是 i 的值,++i 返回的是 i+1 的值
2)i++ 不能用作左值,++i 可以用作左值
根本区别是:能否允许用取地址符号 & 来获取相应的内存地址
1 // ++i 的实现 2 // note:++i运算符重载时不需要加形参 3 int& int::operator++(){ 4 *this += 1; 5 return *this; 6 } 7 8 // i++ 的实现 9 // note:i++ 运算符重载时需要加形参 10 const int int::operator(int){ 11 int oldValue = *this; 12 ++(*this); 13 return oldValue;// 因为返回的是拷贝的临时变量,所以不能是左值 14 }
原文:https://www.cnblogs.com/zpcoding/p/10805377.html