forked from CaptGreg/SenecaOOP345-attic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctormove.cpp
116 lines (100 loc) · 3.3 KB
/
ctormove.cpp
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
#include <iostream>
#include <string>
#include <cstring> // memcpy
#include <exception> // throw std::bad_alloc
using namespace std;
// C++11 introduces R-value &&/std::move operations
void print(int & i) { cout << i << " is a l-value\n"; }
void print(int && i) { cout << i << " is a r-value\n"; }
class X {
const static size_t SIZE = 125000000; // 125 million doubles * sizeof(double) = 1 GByte
double *trouble;
string name;
public:
X() {cout<<"X ctor\n"; trouble = new double[SIZE];} // ctor
X(string n) :name(n) {cout<<"X ctor:"<<name<<"\n"; trouble = new double[SIZE];} // ctor
void setname(string n) { name = n; }
void printtrouble(string label) { cout << label << ":" << name << ".trouble=" << (void*) trouble << "\n"; }
~X() {cout<<"X dtor:"<<name<<"\n"; if(trouble) delete [] trouble;} // dtor
X(X& rhs) { // copy ctor C++98/C++03
cout<<"X copy ctor (deep copy of rhs)\n";
trouble = new double[SIZE];
memcpy(trouble, rhs.trouble, SIZE * sizeof(double)); // deep copy
name = rhs.name;
}
X& operator= (const X& rhs) { // assignment operator C++98/C++03
cout << "assignment operator: " << name << "=" << rhs.name << "\n";
if(this != &rhs) {
if(trouble) // what if we are a zombie?
{ delete [] trouble; trouble = nullptr; }
if(rhs.trouble) { // what if rhs is a zombie?
trouble = new double [ SIZE];
memcpy(trouble, rhs.trouble, SIZE * sizeof(double));
}
name = rhs.name;
} else {
cout << "somebody called assignment operator with same value, eg x=x\n";
}
return *this;
}
X(X&& rhs) { // move ctor C++11
cout<<"X move ctor (move rhs pointer)\n";
trouble = rhs.trouble; rhs.trouble = nullptr;
name = rhs.name; rhs.name.clear();
}
X&& operator= (X&& rhs) { // assignment operator C++11
cout << "move assignment operator: "
<< name << "=" << rhs.name
<< " and " << rhs.name << " is an r-value"
<< "\n";
if(this != &rhs) {
// move pointers
if(trouble) { delete [] trouble; }
trouble = rhs.trouble; rhs.trouble = nullptr;
name = rhs.name; rhs.name.clear();
} else {
cout << "somebody called assignment operator with same value, eg x=x\n";
}
return std::move(*this);
}
};
X global("global");
int main(int argc, char** argv)
{
cout << "main start\n";
try {
// 244 C++98/C++98 copy ctor + assignment operator
int i; // ctor;
i = 27; // assignment operator
int j = i; // copy ctor
j = 19; // assignment operator
j = i; // assignment operator
// C++11 R-Value
print(9);
print(9+5);
print(i);
print(i+6);
print(std::move(i));
print(i);
X x("x"); // ctor 244
x.printtrouble("x");
X y(x); // copy ctor 244
y.setname("y");
x.printtrouble("x");
y.printtrouble("y");
x = y; // assignment operator 244
x.setname("x");
x.printtrouble("x");
y.printtrouble("y");
x = std::move(y); // move assignment operator C++11
x.printtrouble("x");
y.printtrouble("y");
X z(std::move(x)); // move constructor C++11
x.printtrouble("x");
z.printtrouble("z");
} catch(exception& e) {
cerr << "It threw! :-( " << e.what() << "\n";
}
cout << "main finish\n";
return 0;
}