Sta*_*oła 0 c++ move-semantics c++11
在这个例子中,移动语义是如何工作的:
struct test {
int ii[10];
int i;
};
test f() {
test a;
std::cout << "[1] " << &a << std::endl;
return a;
}
int main()
{
test x (f());
std::cout << "[2] " << &x << std::endl;
test x1 (std::move(x));
std::cout << "[3] " << &x1;
}
Run Code Online (Sandbox Code Playgroud)
输出:
[1] 0x7fff50ac70c0
[2] 0x7fff50ac70c0
[3] 0x7fff50ac70f0
Run Code Online (Sandbox Code Playgroud)
为什么x是使用f()的返回值构造的,但是x1的地址与x不同?
编辑
struct test {
std::string s;
}
[...]
std::cout << (*void)&x.s[0];
Run Code Online (Sandbox Code Playgroud)
我想最终我明白了.现在地址是一样的.