C++ copy constructor invocation

前端 未结 5 1680
轮回少年
轮回少年 2021-01-11 15:02

As far as i know, a copy constructor is invoked in the following scenarios :

1) Pass by value
2) Return by value
3) When you create and initialize a new obje         


        
5条回答
  •  [愿得一人]
    2021-01-11 15:41

    When doing x = funct(); the compiler notices that it will be directly returned and thus avoids a useless construction. That's also why you will only get two destructor calls.

    This is a example why sometimes working with "copy" isn't necessarily a lost of performances.

提交回复
热议问题