-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartPtrTest.cpp
More file actions
112 lines (89 loc) · 1.73 KB
/
smartPtrTest.cpp
File metadata and controls
112 lines (89 loc) · 1.73 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Base
{
private:
Base(int i)
: a(i)
{
cout << "Constructor" << endl;
}
Base(const Base& cpy)
{
a = cpy.a;
cout << "copy contructor" << endl;
}
Base(Base&& mv)
{
a = mv.a;
cout << "move contructor" << endl;
}
public:
~Base()
{
cout << "Destructor" << endl;
}
static unique_ptr<Base> CreateUnique(int a);
static shared_ptr<Base> CreateShared(int a);
private:
int a{ 0 };
};
class A
{
public:
virtual void Fun()
{
cout << "call A" << endl;
}
};
class B : public A
{
public:
virtual void Fun() override
{
cout << "call B" << endl;
}
};
unique_ptr<Base> Base::CreateUnique(int a)
{
// auto unique = new Base(a);
return std::move(unique_ptr<Base>( new Base(a) ));
// return unique_ptr<Base>(unique);
}
shared_ptr<Base> Base::CreateShared(int a)
{
return std::move(shared_ptr<Base>( new Base(a) ));
}
int main()
{
// auto pA = make_unique<Base>( Base::Create(3) );
// B* pB = new B;
// auto b = make_unique<B>( );
auto p = Base::CreateShared(3);
// weak_ptr<Base>(p).lock().
B* b = new B;
b->Fun();
A* a = b;
a->Fun();
// delete b;
delete a;
vector<int> vecA;
for (int i = 1; i < 9; ++i)
{
vecA.push_back(i * 10);
}
auto iter = vecA.begin() + 2;
cout << *iter << endl;
cout << *(++iter) << endl;
cout << *(iter++) << endl;
cout << *iter << endl;
vector<int> GG { 3, 2 };
cout << GG.size() << endl;
int foo = 3;
int* pFoo = &foo;
const int* cpFoo = pFoo;
int bg { 3.f };
return 0;
}