When running this program line 55 in the code give these outputs in the terminal:
Copy c
Enough money ($150) for a goat!
Raised:50
Lost:150
Copy c - is for when purchase_goat copies payment in.
Raised:50 - is for when purchase_goat constructs the return Banknote
Lost:150 - is for when purchase_goat destructs payment.
What I don't understand is why there is no output for Banknote c construction. Is it not constructing c with an r-value (purchase_goat return statement)? Then should not the move constructor be called and "Move c" be outputed? Even if not the move constructor is called some constructor should be, but there are no outputs whatsoever (not raised, copy c, or move c). What am I missing?
Pretty sure its an optimization, but I am getting lost in tracing it.
Have your ctors print the address of this in hex, see if that shows that it did a silent move of one object to another without creating an extra?
But that "raised" is for when purchase_goat constructs the return Banknote(line 39). I am looking for the output for banknote c. But there is no "raised", "copy c" or "move c" so how is it being constructed?
If there is any visible difference in the "outcome" of your program (except for diagnostic messages) between the two variants above, then the code of your class is probably wrong and should be fixed!
If there is no difference in the "outcome" of your program, then you can just go with the variant where the compiler eliminated the unnecessary copy operation and be happy about the speed-up :-)