Hi, I'm having problems understanding this 1 example. Author of the book that I'm reading suggested to run this program to see in what order all kinds of constructors and destructor are called.
There are more examples in code but i did't understand this one.
1. I understand that we call copy constructor to initialize (X a) from loc
2. is my output line inside copy funton
3. I'm having problems understanding this line
X(X&& x) move constructor
We just created a variable and the next thing we are trying to do is return this (already initialized) a. Is it because we have to move this a from argument list to a in line where is return a;?
Yes, "return a;" is creating another unnamed temporary value.
Think what happens if "a" was taken by reference instead: if it was, and no unnamed temporary variable was getting constructed, you were going to miss a copyconstruction (causing your function to implicitly return a reference, or even worse, a rvalue reference, acting like std::move, creating more confusing behaviour).
Sorry if I couldn't answer in time.
Yes, every object that has been constructed gets destructed.
You have two constructed objects (copy constr+move constr), so you will see two destructed objects as well.