File tree Expand file tree Collapse file tree 4 files changed +107
-0
lines changed
smart-ptr/weak_ptr/lock-weak-ptr Expand file tree Collapse file tree 4 files changed +107
-0
lines changed Original file line number Diff line number Diff line change 212
212
213
213
## Умные указатели
214
214
215
+ ## std::shared_ptr
216
+
215
217
- [ Сравнение shared_ptr] ( smart-ptr/shared_ptr/cmp-shared-ptr )
216
218
- [ Простейшая реализация shared_from_this функциональности] ( smart-ptr/shared_ptr/shared-from-this )
217
219
- [ Приведение типов умных указателей] ( smart-ptr/shared_ptr/shared-ptr-casting )
218
220
- [ Сброс std::shared_ptr] ( smart-ptr/shared_ptr/shared-ptr-reset )
219
221
- [ Удаление элемента из std::vector< std::shared_ptr<… > >] ( smart-ptr/shared_ptr/vec-with-shared-ptr-items )
220
222
223
+ ## std::weak_ptr
224
+
225
+ - [ Как работают методы expired() и lock() у std::weak_ptr] ( smart-ptr/weak_ptr/lock-weak-ptr )
226
+
221
227
222
228
# Макросы
223
229
Original file line number Diff line number Diff line change
1
+ # ---------------------------------------------------------------------------
2
+ # https://github.com/github/gitignore/blob/master/C++.gitignore
3
+
4
+ # Prerequisites
5
+ * .d
6
+
7
+ # Compiled Object files
8
+ * .slo
9
+ * .lo
10
+ * .o
11
+ * .obj
12
+
13
+ # Precompiled Headers
14
+ * .gch
15
+ * .pch
16
+
17
+ # Compiled Dynamic libraries
18
+ * .so
19
+ * .dylib
20
+ * .dll
21
+
22
+ # Fortran module files
23
+ * .mod
24
+ * .smod
25
+
26
+ # Compiled Static libraries
27
+ * .lai
28
+ * .la
29
+ * .a
30
+ * .lib
31
+
32
+ # Executables
33
+ * .exe
34
+ * .out
35
+ * .app
36
+
37
+ # ---------------------------------------------------------------------------
Original file line number Diff line number Diff line change
1
+ #include < memory>
2
+ #include < iostream>
3
+ #include < cstdlib>
4
+
5
+ struct A {
6
+ virtual ~A () = default ;
7
+
8
+ virtual void whoami () const = 0;
9
+ };
10
+
11
+ class B : public A {
12
+ public:
13
+ B () = default ;
14
+ ~B () override {
15
+ std::cout << " ~B()" << std::endl;
16
+ }
17
+
18
+ void whoami () const override {
19
+ std::cout << " B" << std::endl;
20
+ }
21
+
22
+ private:
23
+ };
24
+
25
+
26
+ void test1 () {
27
+ std::weak_ptr<A> w;
28
+ assert (w.expired ()); // TRUE
29
+ assert (!w.lock ()); // TRUE
30
+ {
31
+ std::shared_ptr<A> s = std::make_shared<B>();
32
+ w = s;
33
+ assert (!w.expired ()); // TRUE
34
+ assert (w.lock ()); // TRUE
35
+ }
36
+ assert (w.expired ()); // TRUE
37
+ assert (!w.lock ()); // TRUE
38
+ }
39
+
40
+
41
+ void test2 () {
42
+ std::weak_ptr<A> w;
43
+ assert (w.expired ()); // TRUE
44
+ auto s = w.lock ();
45
+ assert (!s); // TRUE
46
+ s = std::make_shared<B>();
47
+ assert (s); // TRUE
48
+ assert (w.expired ()); // TRUE
49
+ }
50
+
51
+
52
+ int main (int argc, char const * argv[]) {
53
+ // test1();
54
+ test2 ();
55
+
56
+ return EXIT_SUCCESS;
57
+ }
Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+
3
+ EXE_FILE=" ./a.out"
4
+
5
+ [ -f " $EXE_FILE " ] && rm " $EXE_FILE "
6
+
7
+ clang++ --std=c++17 main.cc -o " $EXE_FILE " && " $EXE_FILE "
You can’t perform that action at this time.
0 commit comments