-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cc
154 lines (106 loc) · 3.5 KB
/
main.cc
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
#include <iostream>
#include <string>
#include <unordered_map>
class MyClass {
public:
MyClass() {
id_ = ++id;
std::cout << "MyClass() #" << id_ << std::endl;
}
// Конструктор копирования:
MyClass(MyClass const &other) {
id_ = ++id;
std::cout << "MyClass const &other #" << id_ << std::endl;
}
// Конструктор перемещения,
// noexcept - для оптимизации при использовании стандартных контейнеров:
MyClass(MyClass &&other) noexcept {
id_ = ++id;
std::cout << "MyClass(MyClass &&other) self.#" << id_ << ", other.#" << other.GetId() << std::endl;
}
// Деструктор:
~MyClass() {
std::cout << "~MyClass() #" << id_ << std::endl;
}
// Оператор присваивания копированием (copy assignment):
MyClass & operator=(MyClass const &other) {
std::cout << "MyClass & operator=(MyClass const &other)" << std::endl;
if (this == &other)
return *this;
return *this;
}
// Оператор присваивания перемещением (move assignment):
MyClass & operator=(MyClass &&other) {
std::cout << "MyClass & operator=(MyClass &&other) #" << id_ << " = #" << other.GetId() << std::endl;
if (this == &other)
return *this;
return *this;
}
int GetId() const {
return id_;
}
private:
int id_ = 0;
static int id;
};
// non-const static data member must be initialized out of line:
int MyClass::id = 0;
void test1() {
std::unordered_map < std::string,
std::unordered_map<std::string, MyClass> > mainMap;
std::unordered_map<std::string, MyClass> innerMap1;
MyClass innerMap1_value1;
// MyClass() #1
std::pair<std::string, MyClass> innerMap1_pair1("innerMap1_key1",
innerMap1_value1);
// MyClass const &other #2
// При добавлении MyClass в STL-контейнеры вызывается copy constructor.
innerMap1.insert( innerMap1_pair1 );
// MyClass const &other #3
}
// ~MyClass() #2
// ~MyClass() #1
// ~MyClass() #3
void test2() {
std::unordered_map < std::string,
std::unordered_map<std::string, MyClass> > mainMap;
mainMap["mainMap_key1"]["innerMap1_key1"] = MyClass();
// MyClass() #1
// MyClass() #2
// MyClass & operator=(MyClass &&other) #2 = #1
// ~MyClass() #1
assert(mainMap["mainMap_key1"]["innerMap1_key1"].GetId() == 2);
std::cout << "end;" << std::endl;
// end;
}
// ~MyClass() #2
void test3() {
std::unordered_map<std::string, MyClass> map;
assert(map.size() == 0); // true
map["key1"];
// MyClass() #1
assert(map.size() == 1); // true
std::cout << map["key1"].GetId() << std::endl;
// 1
}
// ~MyClass() #1
void test4() {
std::unordered_map<std::string, MyClass> map;
map.insert( { "key1", MyClass() } );
// MyClass() #1
// MyClass(MyClass &&other) self.#2, other.#1
// MyClass(MyClass &&other) self.#3, other.#2
// ~MyClass() #2
// ~MyClass() #1
std::cout << "end;" << std::endl;
// end;
}
// ~MyClass() #3
int main(int argc, char const * argv[]) {
//test1();
//test2();
//test3();
//test4();
std::cout << "end." << std::endl;
return 0;
}