| 1 | #include <cstdio> |
|---|---|
| 2 | #include <memory> |
| 3 | #include <string> |
| 4 | |
| 5 | struct User { |
| 6 | int id = 30; |
| 7 | std::string name = "steph"; |
| 8 | }; |
| 9 | |
| 10 | struct NodeS { |
| 11 | std::shared_ptr<NodeS> next; |
| 12 | int value; |
| 13 | }; |
| 14 | |
| 15 | int main() { |
| 16 | std::shared_ptr<int> sp_empty; |
| 17 | std::shared_ptr<int> sp_int = std::make_shared<int>(args: 10); |
| 18 | std::shared_ptr<std::string> sp_str = std::make_shared<std::string>(args: "hello"); |
| 19 | std::shared_ptr<int> &sp_int_ref = sp_int; |
| 20 | std::shared_ptr<int> &&sp_int_ref_ref = std::make_shared<int>(args: 10); |
| 21 | std::shared_ptr<User> sp_user = std::make_shared<User>(); |
| 22 | std::shared_ptr<NodeS> ptr_node = |
| 23 | std::shared_ptr<NodeS>(new NodeS{.next: nullptr, .value: 2}); |
| 24 | ptr_node = std::shared_ptr<NodeS>(new NodeS{.next: std::move(ptr_node), .value: 1}); |
| 25 | |
| 26 | // Construct empty shared_ptr with non-null control field. |
| 27 | std::shared_ptr<int> si(new int(47)); |
| 28 | std::shared_ptr<int> sie(si, nullptr); |
| 29 | |
| 30 | std::puts(s: "// break here"); |
| 31 | |
| 32 | std::weak_ptr<int> wie = sie; |
| 33 | std::weak_ptr<int> wie2 = sie; |
| 34 | |
| 35 | std::puts(s: "// break here"); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 |
