-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.cpp
More file actions
119 lines (93 loc) · 2.43 KB
/
move.cpp
File metadata and controls
119 lines (93 loc) · 2.43 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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
class String {
public:
String() : m_Data(nullptr), m_Size(0) {};
String(const char* str) {
// basic constructor which takes in a string and creates a copy of it of the same size
printf("Created!\n\n");
m_Size = strlen(str);
m_Data = new char[m_Size];
memcpy(m_Data, str, m_Size);
}
String(const String& other) {
// copy constructor
printf("Copied!\n");
m_Size = other.m_Size;
m_Data = new char[m_Size];
memcpy(m_Data, other.m_Data, m_Size);
}
String(String&& other) noexcept {
// move constructor
printf("Moved!\n");
m_Size = other.m_Size;
m_Data = other.m_Data;
// take care of the old object by just rewiring the pointers
other.m_Size = 0;
other.m_Data = nullptr;
}
String& operator = (String&& other) noexcept {
// move assignment operator
printf("Move Assigned!\n\n ");
if (this != &other) {
delete[] m_Data;
m_Size = other.m_Size;
m_Data = other.m_Data;
other.m_Size = 0;
other.m_Data = nullptr;
}
return *this;
}
// destructor
~String() {
printf("Destroyed!\n");
delete[] m_Data;
}
void Print() {
for (uint32_t i = 0; i < m_Size; i++) {
printf("%c", m_Data[i]);
}
printf("\n");
}
private:
char* m_Data;
uint32_t m_Size;
};
class Consumer {
public:
Consumer (const String& name) : m_Name(name) {}
Consumer (String&& name) : m_Name((String&&)name) {}
void PrintName() {
m_Name.Print();
}
private:
String m_Name;
};
int main () {
// String ashwattha = String("Ashwattha");
// // Consumer consumer(String("Dhruva"));
// // consumer.PrintName();
// String dest;
// cout << "Ashwattha : ";
// ashwattha.Print();
// cout << "Dest : ";
// dest.Print();
// dest = move(ashwattha);
// cout << "Ashwattha : ";
// ashwattha.Print();
// cout << "Dest : ";
// dest.Print();
vector<int>* vec1 = new vector<int>{1, 2, 3, 4, 5};
vector<int> vec2 = move(*vec1);
vec1->push_back(6);
for (int i : vec1) {
cout << i << " ";
}
cout << endl;
for (int i : vec2) {
cout << i << " ";
}
delete vec2;
// cin.get();
}