提问人:Thinium 提问时间:10/19/2023 最后编辑:Remy LebeauThinium 更新时间:10/19/2023 访问量:68
交换 2 shared_ptr的内容
swap the content of 2 shared_ptr
问:
交换两个 s 的内容时,仅交换内容。当在交换之前从另一个副本创建时,我很惊讶新副本的内容保持不变。shared_ptr
shared_ptr
shared_ptr
下面是一个示例:
std::shared_ptr<int> foo (new int(10));
std::shared_ptr<int> bar (new int(20));
auto abc = foo;
std::swap (foo, bar);
std::cout << "foo: " << *foo << '\n';
std::cout << "bar: " << *bar << '\n';
std::cout << "abc: " << *abc << '\n';
它打印:
foo: 20
bar: 10
abc: 10
为什么在这种情况下不是 20?鉴于它是 .abc
foo
答:
2赞
463035818_is_not_an_ai
10/19/2023
#1
交换两个shared_ptrs,....的内容时
这不是你的代码所做的事情。 交换指针。它不会交换指尖。如果要交换内容,则需要交换内容:。std::swap(foo,bar)
std::swap(*foo,*bar)
事实上,它是智能指针,它实际上与您的代码无关。此外,动态分配变化不大,因此我们可以使用一个更简单的示例来说明:
int a = 10;
int b = 20;
int* a_ptr = &a;
int* b_ptr = &b;
int* c_ptr = a_ptr;
到目前为止,您有两个指针,分别指向 和 。 是 和 也指向的副本。a_ptr
b_ptr
a
b
c_ptr
a_ptr
a
std::swap (a_ptr,b_ptr);
交换后,您现在指向并指向 .第三个指针不受此影响。它仍然指向 .也,并且没有被修改。a_ptr
b_ptr
a_ptr
b
b_ptr
a
c_ptr
a
a
b
std::cout << "foo: " << *a_ptr << '\n';
std::cout << "bar: " << *b_ptr << '\n';
std::cout << "abc: " << *c_ptr << '\n';
输出将是:
20
10
10
就像在您的代码中一样。
6赞
Remy Lebeau
10/19/2023
#2
shared_ptr
只是指向数据的指针。您不是在交换数据本身,而是在交换指针。
让我们一步一步地看一下:
std::shared_ptr<int> foo (new int(10)); std::shared_ptr<int> bar (new int(20));
在这些语句之后,您有两个指向两个单独分配的指针:int
[ foo ] ---> [10]
[ bar ] ---> [20]
auto abc = foo;
然后,此语句创建指向的同一分配。因此,在交换之前,您现在拥有:abc
int
foo
[ foo ] -+-> [10]
|
[ abc ] -+
[ bar ] ---> [20]
std::swap (foo, bar);
然后,您正在交换 和 持有的指针,但您将指针单独保留。因此,在交换之后,您现在拥有:foo
bar
abc
+---------------+
| |
[ foo ] +-> [10] |
| |
[ abc ] -+ |
| |
[ bar ] -+ [20] <-+
这就是为什么打印 20、打印 10 和打印 10 的原因。*foo
*bar
*abc
评论
abc
是交换前的副本。交换对象不会同时交换该对象的先前副本。您可能会混淆代码(交换每个指针指向的内容)和(交换指向的值)。foo
std::swap(*foo, *bar);