| copy (1) | shared_ptr& operator= (const shared_ptr& x) noexcept;template <class U> shared_ptr& operator= (const shared_ptr<U>& x) noexcept; |
|---|---|
| move (2) | shared_ptr& operator= (shared_ptr&& x) noexcept;template <class U> shared_ptr& operator= (shared_ptr<U>&& x) noexcept; |
| move from (3) | template <class U> shared_ptr& operator= (auto_ptr<U>&& x);template <class U, class D> shared_ptr& operator= (unique_ptr<U,D>&& x); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// shared_ptr::operator= example
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo;
std::shared_ptr<int> bar (new int(10));
foo = bar; // copy
bar = std::make_shared<int> (20); // move
std::unique_ptr<int> unique (new int(30));
foo = std::move(unique); // move from unique_ptr
std::cout << "*foo: " << *foo << '\n';
std::cout << "*bar: " << *bar << '\n';
return 0;
}
*foo: 30 *bar: 20