-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_count.cpp
More file actions
28 lines (26 loc) · 982 Bytes
/
move_count.cpp
File metadata and controls
28 lines (26 loc) · 982 Bytes
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
#include <iostream>
#include <vector>
#include <string>
class Widget {
public:
Widget() = default;
Widget(const Widget &rhs) : data(rhs.data) { ++num_of_copy_constructor_calls; }
Widget(Widget &&rhs) : data(std::move(rhs.data)) { ++num_of_move_constructor_calls; }
std::size_t GetCopyCount() { return num_of_copy_constructor_calls; }
std::size_t GetMoveCount() { return num_of_move_constructor_calls; }
private:
static std::size_t num_of_move_constructor_calls;// = 0;
static std::size_t num_of_copy_constructor_calls;// = 0;
std::vector<std::string> data;
};
std::size_t Widget::num_of_move_constructor_calls = 0;
std::size_t Widget::num_of_copy_constructor_calls = 0;
int main() {
Widget w;
std::cout << w.GetCopyCount() << ' ' << w.GetMoveCount() << '\n';
Widget w2(std::move(w));
std::cout << w2.GetCopyCount() << ' ' << w2.GetMoveCount() << '\n';
Widget w3(w2);
std::cout << w3.GetCopyCount() << ' ' << w3.GetMoveCount() << '\n';
return 0;
}