C/C++ 自增自减的前后置语法区分

1) 前置返回一个引用,后置返回一个对象

// ++i实现代码为:
int& operator++(){
    *this += 1;
    return *this;
}  

 

2) 前置不会产生临时对象,后置必须产生临时对象,临时对象会导致效率降低

//i++实现代码为:                                  
int operator++(int)                                   {
  int temp = *this;                                     
  ++*this;
  return temp;
} 
posted @ 2021-09-01 16:24  默行于世  阅读(52)  评论(0编辑  收藏  举报