June 2025
Intermediate to advanced
1093 pages
33h 24m
English
Let's make a small change to the definition of function in Listing 16.13: pass the parameter by value instead of as a constant reference. As you know, this will copy the object for the function. The rest of the program remains the same.
// https://godbolt.org/z/3T869arc8void function(MyValue paramValue) { std::cout << "(function)\n"; MyValue localValue{"local"};}int main() { MyValue mvalue1{"mvalue1"}; function( MyValue{"temp"} ); function( mvalue1 ); { MyValue mvalue2{"mvalue2"}; }}
Listing 16.13 Passing by value creates objects with the compiler-generated copy constructor.
I added the output of (function) just for orientation; it has nothing to do with the lifetime of the instances.
There is something strange to see in the ...
Read now
Unlock full access