小编Sta*_*oła的帖子

当移动语义与std :: move一起使用时?

在这个例子中,移动语义是如何工作的:

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)

我想最终我明白了.现在地址是一样的.

c++ move-semantics c++11

0
推荐指数
1
解决办法
88
查看次数

标签 统计

c++ ×1

c++11 ×1

move-semantics ×1