Repository navigation
Expand file tree
/
Copy pathsmart_pointers.cpp
More file actions
92 lines (79 loc) · 3.16 KB
/
Copy pathsmart_pointers.cpp
File metadata and controls
92 lines (79 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Smart pointers free heap memory automatically (RAII), so no manual delete:
// - unique_ptr: exactly one owner; cannot be copied, only moved
// - shared_ptr: shared ownership; freed when the last owner goes away
// - weak_ptr: watches a shared_ptr without owning it; breaks reference cycles
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
class Resource {
std::string name;
public:
explicit Resource(std::string n) : name(std::move(n)) { std::cout << " + acquired " << name << '\n'; }
~Resource() { std::cout << " - released " << name << '\n'; }
void use() const { std::cout << " using " << name << '\n'; }
};
std::unique_ptr<Resource> openConnection(const std::string& host) { // a factory hands over ownership
return std::make_unique<Resource>("connection to " + host);
}
void consume(std::unique_ptr<Resource> r) { // takes ownership
r->use();
} // r is destroyed here
struct Child;
struct Parent {
std::string name;
std::vector<std::shared_ptr<Child>> children;
explicit Parent(std::string n) : name(std::move(n)) {}
~Parent() { std::cout << " - parent " << name << " destroyed\n"; }
};
struct Child {
std::string name;
std::weak_ptr<Parent> parent; // a shared_ptr here would form a cycle and leak both objects
explicit Child(std::string n) : name(std::move(n)) {}
~Child() { std::cout << " - child " << name << " destroyed\n"; }
void whoIsMyParent() const {
if (auto p = parent.lock()) std::cout << " " << name << "'s parent is " << p->name << '\n';
else std::cout << " " << name << "'s parent no longer exists\n";
}
};
int main() {
std::cout << "1. unique_ptr\n";
{
auto file = std::make_unique<Resource>("file handle");
file->use();
// auto copy = file; // compile error: unique_ptr cannot be copied
auto owner = std::move(file); // ownership moves; 'file' is now empty
std::cout << " file is " << (file ? "valid" : "empty") << " after the move\n";
consume(std::move(owner));
std::cout << " back in main\n";
auto conn = openConnection("db.local");
conn.reset(); // release early
std::cout << " end of scope\n";
}
std::cout << "\n2. shared_ptr\n";
{
auto config = std::make_shared<Resource>("config");
std::cout << " owners: " << config.use_count() << '\n';
{
auto another = config;
std::cout << " owners: " << config.use_count() << '\n';
}
std::cout << " owners: " << config.use_count() << '\n';
std::cout << " end of scope\n";
}
std::cout << "\n3. weak_ptr breaks reference cycles\n";
std::shared_ptr<Child> survivor;
{
auto parent = std::make_shared<Parent>("Rashid");
auto child = std::make_shared<Child>("Ali");
child->parent = parent;
parent->children.push_back(child);
child->whoIsMyParent();
survivor = child;
std::cout << " end of scope\n";
} // the parent is freed because the child only holds a weak_ptr to it
survivor->whoIsMyParent();
std::cout << "\nEnd of main\n";
return 0;
}