as we known, a movable object is one would not be copied deeply when it be assigned to another one of same type. By this way, we can save a lot of time. But today, I found a phenomenon stange to me. Please view code as following.
#include <string>
#include <iostream>
int main() {
std::string s1 = "s1";
std::string s2 = "s2";
std::cout << " s1[" << ( void* ) &s1[0] << "]:" + s1
<< ", s2[" << ( void* ) &s2[0] << "]:" + s2
<< std::endl;
s1.swap( s2 );
std::cout << " s1[" << ( void* ) &s1[0] << "]:" + s1
<< ", s2[" << ( void* ) &s2[0] << "]:" + s2
<< std::endl;
s2 = std::move(s1);
std::cout << " s1[" << ( void* ) &s1[0] << "]:" + s1
<< ", s2[" << ( void* ) &s2[0] << "]:" + s2
<< std::endl;
return EXIT_SUCCESS; }
After moving, although the contents of strings have been changed, but the address that really storing the data of a string has not been changed.
If the memory addesses would not be changed, can we have a reason to confirm that in fact a deeply copy will be performed instead of just only assigen a pointer to target's member?
thanks! Leon