-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
74 lines (67 loc) · 1.38 KB
/
main.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
#include <iostream>
#include <vector>
#include <utility>
using std::cout;
using std::endl;
using std::vector;
class resource {
int x = 0;
};
class foo
{
public:
foo()
: p{new resource{}}
{
cout << std::hex << std::dec << this << " constructor" << endl;
}
foo(const foo& other)
: p{new resource{*(other.p)}}
{
cout << std::hex << std::dec << this << " copy" << endl;
}
foo(foo&& other)
: p{other.p}
{
other.p = nullptr;
cout << std::hex << std::dec << this << " move" << endl;
}
foo& operator=(foo other)
{
cout << std::hex << std::dec << this << " assign" << endl;
swap(*this, other);
return *this;
}
~foo()
{
cout << std::hex << std::dec << this << " destruct" << endl;
delete p;
}
friend void swap(foo& first, foo& second)
{
using std::swap;
swap(first.p, second.p);
}
private:
resource* p;
};
int main()
{
cout << "foo f1;" << endl;
foo f1;
cout << "foo f2{};" << endl;
foo f2{};
cout << "foo f3(f2);" << endl;
foo f3(f2);
cout << "f2 = f1;" << endl;
f2 = f1;
cout << "foo *fooObj1 = new foo();" << endl;
foo *fooObj1 = new foo();
delete fooObj1;
cout << "foo *fooObj2 = new foo(f1);" << endl;
foo *fooObj2 = new foo(f1);
delete fooObj2;
// vector<foo> v;
// v.push_back(foo());
return 0;
}