-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElisonOptimization.cpp
111 lines (84 loc) · 2.02 KB
/
ElisonOptimization.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
#include <iostream>
#include <vector>
#include <memory.h>
using namespace std;
class Test {
private:
static const int SIZE=100;
int *pBuffer{nullptr};
public:
Test() {
cout << "constructor \n";
pBuffer = new int[SIZE]{};
// memset(pBuffer, 0, sizeof(int)*SIZE);
}
Test(int i) {
cout << "parameterized constructor \n";
pBuffer = new int[SIZE]{};
for(int i=0; i<SIZE; i++) {
pBuffer[i] = 7*1;
}
}
Test(const Test &other) {
cout << "copy constructor \n";
pBuffer = new int[SIZE]{};
memcpy(pBuffer, other.pBuffer, SIZE*sizeof(int));
}
Test(Test &&other) {
cout << "Move constructor \n";
pBuffer = other.pBuffer;
other.pBuffer = nullptr;
}
Test &operator=(const Test &other) {
cout << "assignment \n";
pBuffer = new int[SIZE]{};
memcpy(pBuffer, other.pBuffer, SIZE*sizeof(int));
return *this;
}
~Test() {
cout << "destructor \n";
delete [] pBuffer;
}
friend ostream &operator<<(ostream &out, const Test &test);
};
ostream &operator<<(ostream &out, const Test &test) {
out << "Hello from test";
return out;
}
Test getTest() {
return Test();
}
void check(const Test &value) {
cout << "lValue function! \n";
}
void check(Test &&value) {
cout << "rValue function! \n";
}
int main() {
Test test1 = getTest();
cout << test1 << endl;
vector<Test> vec;
vec.push_back(Test());
// Rvalue (commented code won't work)
int value1 = 7;
int *pValue1 = &value1;
// int *pValue2 = &7;
Test *pTest1 = &test1;
// Test *pTest2 = &getTest();
int *pValue3 = &++value1;
cout << *pValue3 << endl;
// int *pValue4 = &value1++;
// int *s = &(7 + value1);
// -------------------------
// Lvalue references
Test &rTest1 = test1;
// Test &rTest2 = getTest();
const Test &rTest2 = getTest();
Test test2(Test(1));
// Rvalue References
Test <est1 = test1;
Test &&rtest1 = getTest(); // Test() can be used
check(test1);
check(getTest());
return 0;
}