-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.cpp
54 lines (44 loc) · 1 KB
/
demo.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
#include <iostream>
#include "plain_ptr.h"
#include "value_ptr.h"
#include "smart_ptr.h"
using namespace std;
void test_1()
{
int i = 42;
AHasPtr p1(&i, 20);
AHasPtr p2 = p1;
cout << p2.get_ptr_val() << endl;
p1.set_ptr_val(0);
cout << p2.get_ptr_val() << endl;
}
void test_2()
{
int obj = 0;
CHasPtr ptr1(obj, 42);
CHasPtr ptr2(ptr1);
cout << ptr1.get_ptr_val() << ", " << ptr1.get_int() << endl;
cout << ptr2.get_ptr_val() << ", " << ptr2.get_int() << endl;
ptr2.set_ptr_val(6);
ptr2.set_int(48);
cout << "修改以后: " << endl;
cout << ptr1.get_ptr_val() << ", " << ptr1.get_int() << endl;
cout << ptr2.get_ptr_val() << ", " << ptr2.get_int() << endl;
}
void test_3()
{
int obj = 0;
BHasPtr ptr1(&obj, 42);
BHasPtr ptr2(ptr1);
cout << ptr1.get_ptr_val() << ", " << ptr1.get_int() << endl;
cout << ptr2.get_ptr_val() << ", " << ptr2.get_int() << endl;
}
int main()
{
// test_1();
// cout << "值型类: " << endl;
// test_2();
cout << "智能指针: " << endl;
test_3();
return 0;
}