-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_shared_ptr.cpp
43 lines (35 loc) · 1.21 KB
/
test_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
#include "../src/shared_ptr.h"
// #include <memory>
#include <iostream>
using namespace mystl_c;
using namespace std;
int main() {
// 测试空指针行为
{
shared_ptr<int> ptr1(new int(10));
shared_ptr<int> ptr2(nullptr);
ptr2 = ptr1;
cout<< *ptr1 << " " << *ptr2 << " ";
cout << ptr1.use_count() << " " << ptr2.use_count() << endl;
}
// 测试赋值行为
{
// int *p1 = new int(10), *p2 = new int(100);
// shared_ptr<int> ptr1(p1);
// shared_ptr<int> ptr2(ptr1);
// shared_ptr<int> ptr3(p2);
// shared_ptr<int> ptr4(ptr3);
// cout<< *ptr1 << " " << *ptr2 << " " << *ptr3 << " " << *ptr4 << endl;
// cout << ptr1.use_count() << " " << ptr2.use_count() << " " << ptr3.use_count() << " " << ptr4.use_count() << endl;
// // 赋值行为
// ptr1 = ptr3;
// cout<< *ptr1 << " " << *ptr2 << " " << *ptr3 << " " << *ptr4 << endl;
// cout << ptr1.use_count() << " " << ptr2.use_count() << " " << ptr3.use_count() << " " << ptr4.use_count() << endl;
// 正确行为
// 10 10 100 100
// 2 2 2 2
// 100 10 100 100
// 3 1 3 3
}
return 0;
}