forked from furkankirac/cs321-2019-20-fall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek4-app1.cpp
121 lines (93 loc) · 3.14 KB
/
week4-app1.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
117
118
119
120
121
#include <iostream>
#include <vector>
// OOP:
// ctor/dtor
// copy-ctor, copy-assignment
// move-ctor, move-assignment
// inheritance
// user-defined literals
// almost always auto - aaa rule: move type to the right
// unified/universal initialization
template<typename T>
struct Vector
{
size_t size;
T* values;
// default c-tor
Vector() : size(0), values(nullptr) { }
// c-tor #1
Vector(size_t size) : size(size), values(new T[size])
{
for(int i=0; i<size; ++i)
values[i] = T();
}
// d-tor
~Vector() { delete[] values; }
// copy c-tor
Vector(const Vector<T>& other) : size(other.size), values(new T[size])
{
for(int i=0; i<size; ++i)
values[i] = other.values[i];
}
// move c-tor
Vector(Vector<T>&& other) : size(other.size), values(other.values)
{
other.values = nullptr;
}
// copy assignment
Vector<T>& operator=(const Vector<T>& other)
{
size = other.size;
delete[] values;
values = new T[size];
for(int i=0; i<size; ++i)
values[i] = other.values[i];
return *this;
}
// move assignment
Vector<T>& operator=(Vector<T>&& other)
{
size = other.size;
delete[] values;
values = other.values;
other.values = nullptr;
return *this;
}
T& at(size_t index) { return values[index]; }
// below we separated operator[] for read and write
// although this operator is symmetric in its behavior during read and write
// that is not always the case. Consider accessing a node of a linked list like this
// reading a node's value is very simple function, but writing to a node can be complex
// they are in fact two different functions. in the example below difference is not reflected
// but dividing them into two different functions is indeed necessary
// subscript operator that writes to values
T& operator[](size_t index) { return values[index]; }
// subscript operator that promises read-only access
T& operator[](size_t index) const { return values[index]; }
};
Vector<int> doSomething()
{
auto v = Vector<int>(1000);
// ...
return v;
}
int main(int argc, char* argv[])
{
// k is being copy constructed from return value of doSomething()
// hint: there is a mechanism called copy elison that partially solves this costly behavior but...
auto k = doSomething(); // copy c-tor from returned value of doSomething()
k = doSomething(); // copy assignment from returned value of doSomething()
auto v1 = Vector<int>(10);
v1.at(0) = 1000;
v1[1] = 2000;
auto v2 = Vector<int>();
auto v3 = Vector<int>();
v2 = v1; // copy assignment: why? v2 already exists, has been constructed. we assign on top of it
v3 = v2 = v1; // chained copy assignment
// v3.operator(v2.operator=(v1));
auto v4 = Vector<int>(v1); // copy c-tor: why? v4 is being constructed right now from v1
// auto v5 = (Vector&&)v4;
auto v5 = std::move(v4); // move c-tor: why? v5 is being constructed right now stealing from v4
std::cout << v1.at(1) << std::endl;
return 0;
}