-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cc
More file actions
71 lines (61 loc) · 1.69 KB
/
Copy pathmain.cc
File metadata and controls
71 lines (61 loc) · 1.69 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
#include <memory>
#include <exception>
#include <iostream>
#include <tuple>
struct IShape;
using IShape_S = std::shared_ptr<IShape>;
using IShape_W = std::weak_ptr<IShape>;
struct IShape {
virtual ~IShape() = default;
virtual IShape_S shared() = 0;
virtual IShape_W weak() = 0;
};
class Circle : public std::enable_shared_from_this<Circle>,
public IShape
{
public:
~Circle() override = default;
IShape_S shared() override {
return shared_from_this();
}
IShape_W weak() override {
return weak_from_this();
}
};
enum class ShapeType {
Circle
};
class ShapeFactory
{
public:
static IShape* MakeShape(ShapeType shapeType) {
switch (shapeType) {
case ShapeType::Circle:
return new Circle;
default:
return nullptr;
}
assert(false);
}
};
int main(int argc, char const* argv[]) {
{
auto circle = IShape_S(ShapeFactory::MakeShape(ShapeType::Circle)); // !
assert(circle->weak().expired()); // TRUE
try {
assert(circle->shared());
} catch (std::exception& e) {
std::cout << e.what() << std::endl; // bad_weak_ptr
}
}
{
IShape_S circle = std::make_shared<Circle>();
assert(!circle->weak().expired()); // TRUE
assert(circle->shared()); // TRUE
}
std::pair my_pair1(123, "abc");
auto my_pair2 = std::make_pair(123, "a");
return 0;
}
// При создании shared_ptr через конструктор ему можно передать указатель на интерфейс.
// make_shared явно требует инстанцируемый класс.