I am new to C++ and am trying to understand rvalue references and move semantics.
I have written a simple class, Class, which includes a method, fun, which creates an instance of the class in its body and returns it. The class has a copy constructor, a move constructor, a (copy) assignment operator, and a (move) assignment operator.
The following
Class obj2(obj1.fun()); // ??
obj3 = obj2.fun(); // (move) Assignment operator
neither calls the copy constructor nor the move constructor and calls the (move) assignment operator, respectively.
How does obj2 get created? Why doesn't
Class obj2(obj1.fun());
call the move constructor,
Class obj2(std::move(obj1.fun()));
does call the move constructor, and
obj3 = obj2.fun()
calls the (move) assignment operator (without needing to write std::move(obj2.fun())
like in the move constructor case)?
Many thanks!