请你来回答一下++i和i++的区别
不扯一扯运算符重载?
前缀重载是WhateverReturnType T::operator++(),后缀重载是WhateverReturnType T::operator++(int)
实际使用中,尤其是自定义迭代器时,一般都是和原生类型的前后缀自增语义保持一致(++i是左值,i++是右值,类型都和i一致)
class Type {
void increment() { ... }
public:
Type& operator++() { increment(); return *this; }
Type operator++(int) { auto copy = *this; operator++(); return copy; }
};