++i和i++ 的实现:
//++i
int& int::operator++(){
*this +=1;
return *this;
}
//i++
const int int::operator++(int){
int tmp=*this;
++(*this);
return tmp;
}
从实现可以看出:
1. ++i 的返回是做了加1操作,i++返回的是原始的值;
i=0;
a=i++;//0
a=++I;//1
2. ++i 返回的是一个引用,而i++ 返回的是一个临时变量;
++++a = ++(++a);//正确,返回引用可以作左值
a++++ = (a++)++;//错误,因为返回的是临时变量不能作左值
3. i++ 调用了++i;所以++i 会比i++ 效率更高
补充:c++的标准库里对++i 和i++重载基本上都遵循以上情况(如不同iterator)