66、说⼀下 ++i和 i++ 的区别

++i (前置加加)先⾃增 1再返回, i++ (后置加加)先返回 i 再自增1。

前置加加不会产⽣临时对象,后置加加必须产⽣临时对象,临时对象会导致效率降低

++i 实现:

int& int::operator++ (){ 

*this +=1;

 return *this;

 }

i++实现:

const int int::operator(int){

 int oldValue = *this;*

++(*this);

 return oldValue;

 }

Last updated