samedi 5 septembre 2020

Constructors and temporary objects of C++11 functions [closed]

Given :

A class named Object defined here : https://codeshare.io/21edW3

A main_1 defined here : https://codeshare.io/5gg7YR

A main_2 defined here : https://codeshare.io/5D1Dxl

Using the C++11 Standard I encountered an odd behavior.

When it comes to main_1 :

I guess it's a kind of optimization and it's like ok a temporary Object object is created inside the function so C++ might as well keep it instead of deleting it.

When it comes to main_2 :

A temporary Object object is created once again cause of the function argument but this time the copy constructor is invoked instead of acting the same way.Why is that? My only guess is that it has something to do with the one being an Object object declared inside the function and the other being an argument of the function

Any insight would be really helpful.Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire