Just when I thought that I understand the rvalue link, I ran into this problem. The code is probably overly long, but the idea is pretty simple. There is a function main () and returnRValueRef ().
#include <iostream>
Ok, I would expect this code to print “built-in default constructor” (for a) first, then “internal constructor” (for a1), and then the destination statement with rhs.a_ = 4. But the output
testcpp/return_rvalue_ref.cpp:14 inside default constructor testcpp/return_rvalue_ref.cpp:17 inside constructor testcpp/return_rvalue_ref.cpp:39 inside assignment operator (rvalue ref)left[a_=0]right[rhs.a_=0]
Can someone explain why the last line in the output prints right[rhs.a_=0] instead of right[rhs.a_=4] ? I thought that move () just does an lvalue in an rvalue without changing its contents. But I'm obviously missing something.
Many thanks for your help.: -)
Edit: I think I know what could be. Maybe the destructor for a1 in the function returnRValueRef() is called when it goes out of scope (even if it is included in the rvalue), and after that the memory location for a1 (or the rvalue reference for it) contains undefined material! Not sure if this is what happens, but seems plausible.
Yogeshwer sharma
source share