C++ 기초 정리 (shared_ptr 직접 작성, 변환연산자 작동 원리)
태그: C++, Cpp, shared_ptr
카테고리: Cpp
변환연산자를 통해 어떻게 int형 변수에 포인터를 넣을 수 있는지?
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <stack>
#include <assert.h>
#include <crtdbg.h>
template <typename T>
class TSharedPtr
{
class CRef
{
public:
CRef()
{
}
~CRef()
{
if (mPtr)
delete mPtr;
}
public:
T* mPtr = nullptr;
int mRefCount = 0;
};
public:
TSharedPtr()
{
mRef = new CRef;
mRef->mPtr = new T;
++mRef->mRefCount;
}
// 저장을 하기 위한 동적할당. 인자로 포인터를 넣는다.
TSharedPtr(const T& Ptr)
{
// T포인터를 직접 넣어준다는 건, 포인터가 새로 공간이 만들어진다는 것.
mRef = new CRef;
++mRef->mRefCount;
mRef->mPtr = new T;
*mRef->mPtr = Ptr;
}
TSharedPtr(T* Ptr)
{
mRef = new CRef;
++mRef->mRefCount;
mRef->mPtr = Ptr;
}
TSharedPtr(const TSharedPtr<T>& Ptr)
{
// 복사 생성자.
mRef = Ptr.mRef;
if (mRef)
++mRef->mRefCount; // 참조가 일어나므로 참조 카운트 증가.
}
TSharedPtr(const TSharedPtr<T>&& Ptr)
{
// 이동 연산자.
mRef = Ptr.mRef;
Ptr.mRef = nullptr;
}
~TSharedPtr()
{
if (mRef)
{
--mRef->mRefCount;
if (mRef->mRefCount == 0)
{
delete mRef;
}
}
}
public:
void operator = (const TSharedPtr<T>& Ptr)
{
mRef = Ptr.mRef;
if (mRef)
++mRef->mRefCount;
}
void operator = (T* Ptr)
{
if (mRef)
{
delete mRef->mPtr;
delete mRef;
}
mRef = new CRef;
++mRef->mRefCount;
mRef->mPtr = Ptr;
}
void operator = (T Ptr)
{
if (mRef)
{
delete mRef->mPtr;
delete mRef;
}
mRef = new CRef;
mRef->mPtr = new T;
*mRef->mPtr = Ptr;
++mRef->mRefCount;
}
T* operator -> ()
{
return mRef->mPtr;
}
const T* operator -> () const
{
return mRef->mPtr;
}
T& operator * ()
{
return *(mRef->mPtr);
}
const T& operator * () const
{
return *(mRef->mPtr);
}
// 변환 연산자 작성.
operator const T& () const
{
return *(mRef->mPtr);
}
private:
CRef* mRef = nullptr;
public:
// 동적 할당해서 shared_ptr 만드는 함수 작성함.
static const TSharedPtr<T>& make_shared(const T& Data)
{
return TSharedPtr<T>(Data);
}
};
class CTest
{
public:
CTest()
{
}
~CTest()
{
}
public:
int mNum = 100;
};
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
int* Number1 = new int;
TSharedPtr<int> IntPtr;
TSharedPtr<int> IntPtr1 = IntPtr;
TSharedPtr<int> IntPtr2(Number1);
*IntPtr = 500;
int Number = IntPtr; // 변환 연산자로 인해 포인터를 int 타입 변수에 넣는 것도 가능해짐.
*IntPtr2 = 300;
printf("%d\n", *IntPtr);
printf("%d\n", *IntPtr1);
printf("%d\n", *IntPtr2);
CTest* Test3 = new CTest;
TSharedPtr<CTest> Test1(Test3);
TSharedPtr<CTest> Test2 = Test1;
Test1->mNum = 500;
Test2->mNum = 800;
printf("%d\n", Test3->mNum); // Test1, Test2 모두 Test3을 가리키고 있음. 최종적으로 800 출력.
return 0;
}
댓글남기기