-
Notifications
You must be signed in to change notification settings - Fork 2
/
destructors.cpp
162 lines (139 loc) · 3.45 KB
/
destructors.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
#include <memory>
#include <thread>
class User {
private:
int i_;
public:
User(int i) : i_(i){};
~User() { std::cout << "Called destructor for #" << i_ << "\n"; };
int get() { return i_; };
void print() const { std::cout << "User #" << i_ << "\n"; };
};
void sleep() { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); }
User* create(int num) { return new User(num); }
class MaxUserHolder {
private:
User* max_ = nullptr;
public:
void print() const {
std::cout << "Best user #" << (max_ != nullptr ? max_->get() : -1) << "\n";
};
void update(User* user) {
if (max_ == nullptr || (user != nullptr && max_->get() < user->get())) {
max_ = user;
}
}
};
void object_on_stack() {
User u1 = User(12);
User u2(13);
auto u3 = User(14);
}
void object_on_heap() {
User* u1 = new User(12);
auto u2 = new User(12);
delete u1;
delete u2;
}
void ownership_problem() {
MaxUserHolder holder;
srand(time(NULL));
for (int i = 0; i < 200; i++) {
auto u1 = create(rand());
u1->print();
holder.update(u1);
// delete u1;
}
holder.print();
}
class RandomLetters {
private:
char* arr_;
int len_;
public:
RandomLetters(const RandomLetters& other) {
std::cout << "Calling copy ctor for " << other.arr_ << "\n";
len_ = other.len_;
arr_ = new char[len_];
std::copy(other.arr_, other.arr_ + len_, arr_);
};
RandomLetters(int num) : len_(num) {
srand(time(NULL));
arr_ = new char[num];
for (int i = 0; i < num - 1; ++i) {
arr_[i] = 97 + rand() % 25;
}
arr_[num - 1] = 0;
std::cout << "Called ctor for " << arr_ << "(" << (void*)arr_ << ")\n";
};
~RandomLetters() {
std::cout << "Calling destructor for " << arr_ << "(" << (void*)arr_ << ")"
<< " \n";
delete[] arr_;
};
RandomLetters& operator=(const RandomLetters& other) {
std::cout << "Copy assignment operator for " << arr_ << "\n";
if (this != &other) {
RandomLetters tmp(other);
std::swap(arr_, tmp.arr_);
std::swap(len_, tmp.len_);
}
return *this;
};
const char* get() { return arr_; };
};
void destructor() {
auto r1 = RandomLetters(10);
auto r2 = RandomLetters(20);
std::cout << "r1: " << r1.get() << ", r2: " << r2.get() << "\n";
}
void destructor2() {
auto r1 = RandomLetters(10);
auto r2 = RandomLetters(20);
r2 = r1;
std::cout << "r1: " << r1.get() << ", r2: " << r2.get() << "\n";
}
class MaxUserHolderV2 {
private:
std::shared_ptr<User> max_;
public:
void print() const {
std::cout << "Best user #" << (max_ ? max_->get() : -1) << "\n";
};
void update(std::shared_ptr<User> user) {
if (!max_ || (user && max_->get() < user->get())) {
max_ = user;
}
}
};
void ownership_problem2() {
MaxUserHolderV2 holder;
srand(time(NULL));
for (int i = 0; i < 5; i++) {
auto u1 = std::make_shared<User>(rand());
u1->print();
holder.update(u1);
// delete u1;
}
holder.print();
}
std::shared_ptr<User> create_ptr(int arg) {
return std::shared_ptr<User>(new User(arg));
}
void smart_ptrs1() {
std::shared_ptr<User> u1 = create_ptr(10);
std::shared_ptr<User> u2 = u1;
std::cout << "Owners: " << u1.use_count() << "\n";
u1->print();
u2->print();
}
void smart_ptrs2() {
std::unique_ptr<User> u1 = std::make_unique<User>(12);
// doesn't work: std::unique_ptr<User> u2 = u1;
u1->print();
}
int main() { smart_ptrs2(); }