-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path13.53.cpp
113 lines (106 loc) · 2.57 KB
/
13.53.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
/*
* Exercise 13.53: As a matter of low-level efficiency, the HasPtr assignment
* operator is not ideal. Explain why. Implement a copy-assignment and
* move-assignment operator for HasPtr and compare the operations executed in
* your new move-assignment operator versus the copy-and-swap version.
*
* By Faisal Saadatmand
*
* Answer: in terms of low-level efficiency, the copy-and-swap assignment
* involves more functions, which could affect performance due to the overhead
* associated with calling a function.
*
* The following is comparison of the call stacks of the two implementations,
* read top to bottom:
*
* copy-and-swap: copy
* HasPtr(HasPtr &)
* operator=(HasPtr)
* swap(HasPtr &, HasPtr &)
* ~HasPtr()
* ~HasPtr()
* ~HasPtr()
*
* copy assignment operator:
* HasPtr(HasPtr &)
* operator=(const HasPtr &)
* ~HasPtr()
* ~HasPtr()
*
* copy-and-swap: move
* HasPtr(HasPtr &&)
* operator=(HasPtr)
* swap(HasPtr &, HasPtr &)
* ~HasPtr()
* ~HasPtr()
* ~HasPtr()
*
* move assignment operator:
* operator=(HasPtr &&)
* ~HasPtr()
* ~HasPtr()
*/
#include <algorithm>
#include <iostream>
#include <string>
class HasPtr {
friend std::ostream& print(std::ostream &, const HasPtr &);
friend void swap(HasPtr &, HasPtr &);
public:
HasPtr(const std::string &s = std::string())
: ps(new std::string(s)), i(0) {}
HasPtr(const HasPtr& rhs)
: ps(new std::string(*rhs.ps)), i(rhs.i)
{std::cout << "HasPtr(HasPtr &)\n";}
HasPtr(HasPtr &&p) noexcept
: ps(p.ps), i(p.i) { p.ps = 0; std::cout << "HasPtr(HasPtr &&)\n"; }
HasPtr& operator=(const HasPtr &);
HasPtr& operator=(HasPtr &&) noexcept;
// HasPtr& operator=(HasPtr rhs) { swap(*this,rhs); return *this; }
~HasPtr() { delete ps; std::cout << "~HasPtr()\n"; }
bool operator<(const HasPtr &rhs) { return *ps < *rhs.ps; }
private:
std::string *ps;
int i;
};
HasPtr&
HasPtr::operator=(const HasPtr &rhs)
{
auto newp = new std::string(*rhs.ps);
delete ps;
ps = newp;
i = rhs.i;
std::cout << "operator=(const HasPtr &)\n";
return *this;
}
HasPtr&
HasPtr::operator=(HasPtr &&rhs) noexcept
{
if (this != &rhs) {
delete ps;
ps = rhs.ps;
i = rhs.i;
rhs.ps = nullptr;
}
std::cout << "operator=(HasPtr &&)\n";
return *this;
}
inline void swap(HasPtr &lhs, HasPtr &rhs)
{
using std::swap;
swap(lhs.ps, rhs.ps);
swap(lhs.i, rhs.i);
std::cout << "swap(HasPtr &, HasPtr &)\n";
}
std::ostream& print(std::ostream &os, const HasPtr &p)
{
os << p.ps << ' ' << *p.ps << ' ' << p.i;
return os;
}
int main()
{
HasPtr hp("hp string"), hp2("hp2 string");
hp = hp2;
std::cout << "done\n";
return 0;
}