Content-Length: 474929 | pFad | http://github.com/gusenov/examples-cpp/commit/b0ec903f840ec09036ce165ce8d34bf437ef6458

16 Как работают методы expired() и lock() у std::weak_ptr. · gusenov/examples-cpp@b0ec903 · GitHub
Skip to content

Commit b0ec903

Browse files
committed
Как работают методы expired() и lock() у std::weak_ptr.
1 parent 3aa2f4d commit b0ec903

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,18 @@
212212

213213
## Умные указатели
214214

215+
## std::shared_ptr
216+
215217
- [Сравнение shared_ptr](smart-ptr/shared_ptr/cmp-shared-ptr)
216218
- [Простейшая реализация shared_from_this функциональности](smart-ptr/shared_ptr/shared-from-this)
217219
- [Приведение типов умных указателей](smart-ptr/shared_ptr/shared-ptr-casting)
218220
- [Сброс std::shared_ptr](smart-ptr/shared_ptr/shared-ptr-reset)
219221
- [Удаление элемента из std::vector<std::shared_ptr<…>>](smart-ptr/shared_ptr/vec-with-shared-ptr-items)
220222

223+
## std::weak_ptr
224+
225+
- [Как работают методы expired() и lock() у std::weak_ptr](smart-ptr/weak_ptr/lock-weak-ptr)
226+
221227

222228
# Макросы
223229

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# ---------------------------------------------------------------------------
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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"

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/gusenov/examples-cpp/commit/b0ec903f840ec09036ce165ce8d34bf437ef6458

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy