Why is Copy constructor being called only when we return *this?

前端 未结 2 1694
逝去的感伤
逝去的感伤 2020-12-19 18:47
Class Cents(){  
 int m_val;  
 public:  
  Cents(int x=0){ cout<<\"Constructor\";}
  Cents(const Cents& src){ cout<<\"Copy constructor\"}
  Cents Ad         


        
2条回答
  •  独厮守ぢ
    2020-12-19 19:11

    This happens because most of the compilers perform Return Value Optimization (aka RVO) to save on copying.

提交回复
热议问题