-
Notifications
You must be signed in to change notification settings - Fork 1
/
E31.h
54 lines (47 loc) · 1.09 KB
/
E31.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
#ifndef E31_H
#define E31_H
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
class HasPtr
{
friend void swap(HasPtr&, HasPtr&);
friend bool operator<(const HasPtr&, const HasPtr&);
public:
HasPtr(const string &s): ps(new string(s)), i(0){}
HasPtr(const HasPtr& hp) : ps(new string(*hp.ps)), i(0) {}
// HasPtr& operator=(const HasPtr&);
HasPtr& operator=(HasPtr);
string& getPs() const { return *ps; }
~HasPtr() { delete ps; };
private:
string *ps;
int i;
};
// HasPtr& HasPtr::operator=(const HasPtr& hp){
// auto newp = new string(*hp.ps);
// delete ps;
// ps = newp;
// i = hp.i;
// return *this;
// }
HasPtr& HasPtr::operator=(HasPtr hp){
swap(*this, hp);
return *this;
}
/*
The swap function only exchange the pointer of each other.
No need for the allocate the memory, so it benifis the efficient
*/
void swap(HasPtr& hpl, HasPtr& hpr){
using std::swap;
swap(hpl.ps, hpr.ps);
swap(hpl.i, hpr.i);
cout << "calling swap function is done !" << endl;
}
bool operator<(const HasPtr& hpl, const HasPtr& hpr){
return *hpl.ps < *hpr.ps;
}
#endif