-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathstdmove.cpp
More file actions
155 lines (138 loc) · 3.17 KB
/
stdmove.cpp
File metadata and controls
155 lines (138 loc) · 3.17 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
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
// stdmove.cpp using std::move()
#include <iostream>
#include <utility>
// interface
class Useless
{
private:
int n;
char *pc;
static int ct;
void ShowObject() const;
public:
Useless();
explicit Useless(int k);
Useless(int k,char ch);
Useless(const Useless & f); // 常规复制构造器
Useless(Useless && f);
~Useless();
Useless operator+(const Useless & f) const;
Useless & operator=(const Useless & f); // 复制赋值
Useless & operator=(Useless && f); // 移动赋值
void ShowData() const;
};
// 实现
int Useless::ct = 0;
Useless::Useless()
{
ct++;
n = 0;
pc = nullptr;
}
Useless::Useless(int k) : n(k)
{
++ct;
pc = new char[n];
}
Useless::Useless(int k,char ch) : n(k)
{
++ct;
pc = new char [n];
for (int i = 0; i < n ; i++)
pc[i] = ch;
}
Useless::Useless(const Useless & f) : n(f.n)
{
++ct;
pc = new char[n];
for (int i = 0; i < n; i++)
pc[i] = f.pc[i];
}
Useless::Useless(Useless && f) : n(f.n)
{
++ct;
pc = f.pc; // steal address
f.pc = nullptr; // 给原对象设置为返回为空
f.n = 0;
}
Useless::~Useless()
{
delete [] pc;
}
Useless & Useless::operator=(const Useless & f) // 复制赋值
{
std::cout << "copy assignment operator called : \n";
if (this == &f)
return *this;
delete [] pc;
n = f.n;
pc = new char[n];
for (int i = 0; i < n; i++)
pc[i] = f.pc[i];
return *this;
}
Useless & Useless::operator=(Useless && f) // move 赋值
{
std::cout << " move assignment operator called : \n";
if (this == &f)
return *this;
delete [] pc;
n = f.n;
pc = f.pc;
f.n = 0;
f.pc = nullptr;
return *this;
}
Useless Useless::operator+(const Useless & f) const
{
Useless temp = Useless(n + f.n);
for (int i = 0; i < n; i++)
temp.pc[i] = pc[i];
for (int i = n; i < temp.n; i++)
temp.pc[i] = f.pc[i - n];
return temp;
}
void Useless::ShowObject() const
{
std::cout << "Number of elements : " << n;
std::cout << " Data Address : " << (void *) pc <<std::endl;
}
void Useless::ShowData() const
{
if (n == 0)
std::cout << "(obejct empty)";
else
for (int i = 0; i < n; i++)
std::cout << pc[i];
std::cout << std::endl;
}
// application
int main()
{
using std::cout;
{
Useless one(10,'x');
Useless two = one +one; // 调用 move构造函数
cout << "Obejct one: ";
one.ShowData();
cout << "Object two: ";
two.ShowData();
Useless three, four;
cout << "Three = one \n";
three = one; // 自动复制赋值
cout << "now object three = ";
three.ShowData();
cout << "and object one = ";
one.ShowData();
cout << "four = one + two \n";
four = one + two;
cout << "now object four = ";
four.ShowData();
cout << "four = move(one) \n";
four = std::move(one); // 强制移动赋值
cout << "now object four = ";
four.ShowData();
cout << "and object one = ";
one.ShowData();
}
}