-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ex13_53.cpp
61 lines (53 loc) · 1.23 KB
/
ex13_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
#include "ex13_53.h"
#include <iostream>
inline void swap(HasPtr& lhs, HasPtr& rhs)
{
using std::swap;
swap(lhs.ps, rhs.ps);
swap(lhs.i, rhs.i);
std::cout << "call swap" << std::endl;
}
HasPtr::HasPtr(const std::string& s) : ps(new std::string(s)), i(0)
{
std::cout << "call constructor" << std::endl;
}
HasPtr::HasPtr(const HasPtr& hp) : ps(new std::string(*hp.ps)), i(hp.i)
{
std::cout << "call copy constructor" << std::endl;
}
HasPtr::HasPtr(HasPtr&& p) noexcept : ps(p.ps), i(p.i)
{
p.ps = 0;
std::cout << "call move constructor" << std::endl;
}
HasPtr& HasPtr::operator=(HasPtr rhs)
{
swap(*this, rhs);
return *this;
}
// HasPtr& HasPtr::operator=(const HasPtr &rhs)
//{
// auto newp = new std::string(*rhs.ps);
// delete ps;
// ps = newp;
// i = rhs.i;
// std::cout << "call copy assignment" << std::endl;
// return *this;
//}
// HasPtr& HasPtr::operator=(HasPtr &&rhs) noexcept
//{
// if (this != &rhs)
// {
// delete ps;
// ps = rhs.ps;
// i = rhs.i;
// rhs.ps = nullptr;
// std::cout << "call move assignment" << std::endl;
// }
// return *this;
//}
HasPtr::~HasPtr()
{
std::cout << "call destructor" << std::endl;
delete ps;
}