-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperatoroverloading.cpp
More file actions
190 lines (167 loc) · 3.25 KB
/
operatoroverloading.cpp
File metadata and controls
190 lines (167 loc) · 3.25 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <memory>
class Integer
{
int *m_pInt;
public:
// Default constructor
Integer();
// Parameterized constructor
Integer(int value);
// Copy constructor
Integer(const Integer &obj);
// Move constructor
Integer(Integer &&obj);
int GetValue() const;
void SetValue(int value);
~Integer();
Integer &operator++();
Integer operator++(int);
bool operator==(const Integer &a) const;
// Copy assignment
Integer &operator=(const Integer &a);
// Move assignment
Integer &operator=(Integer &&a);
Integer operator+(const Integer &a) const;
void operator()();
};
Integer operator+(int x, const Integer &y);
std::ostream &operator<<(std::ostream &out, const Integer &a);
std::istream &operator>>(std::istream &input, Integer &a);
class IntPtr
{
Integer *m_p;
public:
IntPtr(Integer *p) : m_p(p)
{
}
~IntPtr()
{
delete m_p;
}
Integer *operator->()
{
return m_p;
}
Integer &operator*()
{
return *m_p;
}
};
void CreateInteger()
{
std::unique_ptr<Integer> p(new Integer);
// auto p2(p);
(*p).SetValue(3);
// std::cout << p->GetValue() << std::endl;
}
void Process(Integer val)
{
}
int main()
{
using namespace std;
Integer a(3);
auto b(std::move(a));
// std::cout << a << std::endl;
Process(std::move(a));
cout << "End of Program\n";
return 0;
}
Integer::Integer()
{
std::cout << "Integer()" << std::endl;
m_pInt = new int(0);
}
Integer::Integer(int value)
{
std::cout << "Integer(int)" << std::endl;
m_pInt = new int(value);
}
Integer::Integer(const Integer &obj)
{
std::cout << "Integer(const Integer&)" << std::endl;
m_pInt = new int(*obj.m_pInt);
}
Integer::Integer(Integer &&obj)
{
std::cout << "Integer(int&&)" << std::endl;
m_pInt = obj.m_pInt;
obj.m_pInt = nullptr;
}
int Integer::GetValue() const
{
return *m_pInt;
}
void Integer::SetValue(int value)
{
*m_pInt = value;
}
Integer::~Integer()
{
std::cout << "~Integer()" << std::endl;
delete m_pInt;
}
Integer &Integer::operator++()
{
++(*m_pInt);
return *this;
// TODO: insert return statement here
}
Integer Integer::operator++(int)
{
Integer temp(*this);
++(*m_pInt);
return temp;
}
bool Integer::operator==(const Integer &a) const
{
return *m_pInt == *a.m_pInt;
}
Integer &Integer::operator=(const Integer &a)
{
if (this != &a)
{
delete m_pInt;
m_pInt = new int(*a.m_pInt);
}
return *this;
}
Integer &Integer::operator=(Integer &&a)
{
if (this != &a)
{
delete m_pInt;
m_pInt = a.m_pInt;
a.m_pInt = nullptr;
}
return *this;
}
Integer Integer::operator+(const Integer &a) const
{
Integer temp;
*temp.m_pInt = *m_pInt + *a.m_pInt;
return temp;
}
void Integer::operator()()
{
std::cout << *m_pInt << std::endl;
}
Integer operator+(int x, const Integer &y)
{
Integer temp;
temp.SetValue(x + y.GetValue());
return temp;
}
std::ostream &operator<<(std::ostream &out, const Integer &a)
{
out << a.GetValue();
return out;
}
std::istream &operator>>(std::istream &input, Integer &a)
{
int x;
input >> x;
a.SetValue(x);
return input;
}