-
Notifications
You must be signed in to change notification settings - Fork 0
/
self-assign.cpp
75 lines (66 loc) · 1.16 KB
/
self-assign.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
#include <new>
#include <memory>
#include <atomic>
#include <thread>
#include <mutex>
#include <future>
#include <utility>
#include <tuple>
#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <functional>
#include <regex>
#include <bitset>
#include <iostream>
using namespace std;
/**
* handle self-assignment in operator=
*
* operator overload: exploite people's intuition and reduce learning curve
*/
// solution 1: implement assignment operator
class collar
{
};
class dog
{
collar *pCollar;
dog &operator=(const dog &rhs)
{
if (this == &rhs)
{
return *this;
}
collar *pOrigCollar = pCollar;
pCollar = new collar(*rhs.pCollar);
delete pOrigCollar;
return *this;
}
};
// solution 2: delegation
class cat
{
collar *pCollar;
cat &operator=(const cat &rhs)
{
*pCollar = *rhs.pCollar;
// member by member copying of collars or call collar's operator=
return *this;
}
};
int main()
{
return 0;
}