Content-Length: 482108 | pFad | http://github.com/gusenov/examples-cpp/commit/ff8e93e9d5e1d258e21a2e699dd3221ca0b2e176

4A Различия в создании shared_ptr через конструктор и make_shared. · gusenov/examples-cpp@ff8e93e · GitHub
Skip to content

Commit ff8e93e

Browse files
committed
Различия в создании shared_ptr через конструктор и make_shared.
1 parent 1f6afe3 commit ff8e93e

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
- [Сброс std::shared_ptr](smart-ptr/shared_ptr/shared-ptr-reset)
233233
- [Удаление элемента из std::vector<std::shared_ptr<…>>](smart-ptr/shared_ptr/vec-with-shared-ptr-items)
234234
- [std::shared_ptr не вызывающий деструктор](smart-ptr/shared_ptr/null-deleter)
235+
- [Различия в создании shared_ptr через конструктор и make_shared](smart-ptr/shared_ptr/shared_ptr-vs-make_shared)
235236

236237
## std::weak_ptr
237238

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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <memory>
2+
#include <exception>
3+
#include <iostream>
4+
#include <tuple>
5+
6+
struct IShape;
7+
using IShape_S = std::shared_ptr<IShape>;
8+
using IShape_W = std::weak_ptr<IShape>;
9+
10+
struct IShape {
11+
virtual ~IShape() = default;
12+
virtual IShape_S shared() = 0;
13+
virtual IShape_W weak() = 0;
14+
};
15+
16+
class Circle : public std::enable_shared_from_this<Circle>,
17+
public IShape
18+
{
19+
public:
20+
~Circle() override = default;
21+
IShape_S shared() override {
22+
return shared_from_this();
23+
}
24+
IShape_W weak() override {
25+
return weak_from_this();
26+
}
27+
};
28+
29+
enum class ShapeType {
30+
Circle
31+
};
32+
33+
class ShapeFactory
34+
{
35+
public:
36+
static IShape* MakeShape(ShapeType shapeType) {
37+
switch (shapeType) {
38+
case ShapeType::Circle:
39+
return new Circle;
40+
default:
41+
return nullptr;
42+
}
43+
assert(false);
44+
}
45+
};
46+
47+
int main(int argc, char const* argv[]) {
48+
{
49+
auto circle = IShape_S(ShapeFactory::MakeShape(ShapeType::Circle)); // !
50+
assert(circle->weak().expired()); // TRUE
51+
try {
52+
assert(circle->shared());
53+
} catch (std::exception& e) {
54+
std::cout << e.what() << std::endl; // bad_weak_ptr
55+
}
56+
}
57+
58+
{
59+
IShape_S circle = std::make_shared<Circle>();
60+
assert(!circle->weak().expired()); // TRUE
61+
assert(circle->shared()); // TRUE
62+
}
63+
64+
std::pair my_pair1(123, "abc");
65+
auto my_pair2 = std::make_pair(123, "a");
66+
67+
return 0;
68+
}
69+
70+
// При создании shared_ptr через конструктор ему можно передать указатель на интерфейс.
71+
// make_shared явно требует инстанцируемый класс.
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="./program.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/ff8e93e9d5e1d258e21a2e699dd3221ca0b2e176

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy