-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathExer13_54_HasPtr.h
63 lines (63 loc) · 1.99 KB
/
Exer13_54_HasPtr.h
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
// Warning: overloaded assignment operators are ambiguous.
#ifndef HASPTR_H
#define HASPTR_H
#include <iostream>
#include <string>
#include <algorithm>
class HasPtr{
// swap on page 517
friend void swap(HasPtr&, HasPtr&);
// relational operator required by exercise 13.31
friend bool operator<(const HasPtr&, const HasPtr&);
public:
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { i = ps->size(); }
// here, we can use private members directly! Because this is a member
// function of class HasPtr. They can access any member of the class. Review
// member functions, access control and class scope for more details.
HasPtr(const HasPtr &hp) : ps(new std::string(*(hp.ps))), i(hp.i) { }
// move constructor from page 540
HasPtr(HasPtr &&p) noexcept : ps(p.ps), i(p.i) { p.ps=0; }
HasPtr& operator=(HasPtr rhs) {
swap(*this, rhs);
std::cout << "HasPtr& operator=(HasPtr) is called" << std::endl;
return *this;}
HasPtr& operator=(HasPtr&&) noexcept;
std::ostream& print(std::ostream&) const;
// destructor required by exercise 13.11
~HasPtr() { delete ps; }
private:
std::string *ps;
int i;
};
HasPtr& HasPtr::operator=(HasPtr &&rhs) noexcept
{
if(this != &rhs)
{
ps = rhs.ps;
i = rhs.i;
rhs.ps = 0;
}
std::cout << "HasPtr& HasPtr::operator=(HasPtr&&) is called" << std::endl;
return *this;
}
bool operator<(const HasPtr &lhs, const HasPtr &rhs)
{
return *lhs.ps < *rhs.ps;
}
std::ostream& HasPtr::print(std::ostream &os) const
{
if(ps)
os << *ps;
return os;
}
inline void swap(HasPtr &lhs, HasPtr &rhs)
{
using std::swap;
swap(lhs.ps, rhs.ps);
swap(lhs.i, rhs.i);
std::cout << "two HasPtr objects are swapped" << std::endl; // required by exercise 13.30
}
#endif
// value-like copy and assign required by exercise 13.22
// Note: both copy and copy-assign member allocate new memory, so they make the
// class behave like a value.