-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_client.cpp
More file actions
58 lines (47 loc) · 1.22 KB
/
widget_client.cpp
File metadata and controls
58 lines (47 loc) · 1.22 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
#include <memory>
#include <string>
#include <vector>
class Gadget {
private:
int id_;
};
class Widget {
public:
Widget();
~Widget();
Widget(const Widget &rhs);
Widget& operator=(const Widget &rhs);
Widget(Widget &&rhs);
Widget& operator=(Widget &&rhs);
private:
struct implementation;
std::unique_ptr<implementation> implementation_ptr;
};
struct Widget::implementation {
std::string name;
std::vector<double> data;
Gadget g1, g2, g3;
};
Widget::Widget() : implementation_ptr(std::make_unique<implementation>()) {}
Widget::~Widget() = default;
Widget::Widget(const Widget &rhs) : implementation_ptr(nullptr) {
if (rhs.implementation_ptr)
implementation_ptr = std::make_unique<implementation>(*rhs.implementation_ptr);
}
Widget& Widget::operator=(const Widget &rhs) {
if (!rhs.implementation_ptr)
implementation_ptr.reset();
else if (!implementation_ptr)
implementation_ptr = std::make_unique<implementation>(*rhs.implementation_ptr);
else
*implementation_ptr = *rhs.implementation_ptr;
return *this;
}
Widget::Widget(Widget &&rhs) = default;
Widget& Widget::operator=(Widget &&rhs) = default;
int main() {
Widget w1;
auto w2(std::move(w1));
w1 = std::move(w2);
return 0;
}