-
Notifications
You must be signed in to change notification settings - Fork 350
/
unique_ptr.cpp
64 lines (57 loc) · 1.63 KB
/
unique_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
#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::unique_ptr<MyTime> mt1(new MyTime(10));
std::unique_ptr<MyTime> mt2 = std::make_unique<MyTime>(80); //c++17
std::cout << "mt1: " <<*mt1 << std::endl;
std::cout << "mt2: " <<*mt2 << std::endl;
// std::unique_ptr<MyTime> mt3 = mt1; // error
std::unique_ptr<MyTime> mt3 = std::move(mt1);
// std::shared_ptr<MyTime> mt3 = std::move(mt1);//okay
// std::cout << "mt1: " <<*mt1 << std::endl;
std::cout << "mt3: " <<*mt3 << std::endl;
return 0;
}