forked from ShiqiYu/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_ptr.cpp
77 lines (70 loc) · 2.06 KB
/
shared_ptr.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
#include <iostream>
#include <memory>
class MyTime
{
int hours;
int minutes;
public:
MyTime(): hours(0), minutes(0)
{
std::cout << "Constructor MyTime()" << std::endl;
}
MyTime(int m): hours(0), minutes(m)
{
std::cout << "Constructor MyTime(int)" << std::endl;
this->hours += this->minutes / 60;
this->minutes %= 60;
}
MyTime(int h, int m): hours(h), minutes(m)
{
std::cout << "Constructor MyTime(int,int)" << std::endl;
this->hours += this->minutes / 60;
this->minutes %= 60;
}
~MyTime()
{
std::cout << "Destructor MyTime(). Bye!" << std::endl;
}
MyTime operator+(int m) const
{
MyTime sum;
sum.minutes = this->minutes + m;
sum.hours = this->hours;
sum.hours += sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
friend std::ostream & operator<<(std::ostream & os, const MyTime & t)
{
std::string str = std::to_string(t.hours) + " hours and "
+ std::to_string(t.minutes) + " minutes.";
os << str;
return os;
}
};
int main()
{
// std::shared_ptr<MyTime> mt0 = new MyTime(0,70); //error
// MyTime * mt1 = std::make_shared<MyTime>(1, 70); //error
// {
// std::shared_ptr<MyTime> mt1(new MyTime(10));
// std::cout << *mt1 << std::endl;
// }
{
std::shared_ptr<MyTime> mt1 = std::make_shared<MyTime>(1, 70);
std::shared_ptr<MyTime> mt2 = mt1;
std::shared_ptr<MyTime> mt3 = mt2;
std::cout << "mt1: " << *mt1 << std::endl;
std::cout << "mt2: " << *mt2 << std::endl;
std::cout << "mt3: " << *mt3 << std::endl;
std::cout << "use_count() = " << mt2.use_count() << std::endl;
{
auto mt4 = mt3;
*mt4 = *mt4 + 50;
std::cout << "use_count() = " << mt3.use_count() << std::endl;
}
std::cout << "mt3: " << *mt3 << std::endl;
std::cout << "use_count() = " << mt3.use_count() << std::endl;
}
return 0;
}