Skip to content

Commit ed6dc2d

Browse files
committed
Smart Pointer
Smart pointer types in C++
1 parent 47e20fb commit ed6dc2d

19 files changed

+184
-146
lines changed

Debtor 2/Debtor 2.cpp

-83
This file was deleted.

Debtor 2/Debtor.h

-51
This file was deleted.

Smart Pointer/Smart Pointer.cpp

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
//Smart Pointers
5+
//auto_ptr
6+
7+
//weak_ptr
8+
//unique_ptr
9+
//shared_ptr
10+
11+
class Entity {
12+
int _value;
13+
public:
14+
Entity(int value) { _value = value;
15+
cout << "Konstructor" << endl; }
16+
void doSomething() { cout << "Do something" << endl; }
17+
~Entity(){ cout << "Destructor" << endl; }
18+
19+
};
20+
21+
void foo(Entity obj) {
22+
cout << "foo function" << endl;
23+
}
24+
25+
template <typename T>
26+
class SmartPointer {
27+
T* _ptr;
28+
public:
29+
SmartPointer(T* ptr=NULL){
30+
cout << "Constructor->" << endl;
31+
_ptr = ptr;
32+
}
33+
34+
void reset() {
35+
delete _ptr;
36+
_ptr = NULL;
37+
}
38+
39+
void relaese() {
40+
_ptr = NULL;
41+
}
42+
43+
T* get() {
44+
return _ptr;
45+
}
46+
47+
48+
SmartPointer(SmartPointer<T>& right) noexcept {
49+
cout << "Copy ConstructorS\n";
50+
_ptr = right._ptr;
51+
right._ptr = NULL;
52+
53+
}
54+
55+
SmartPointer<T>& operator=(SmartPointer<T>& right) noexcept {
56+
cout << "Copy Assign Operator\n";
57+
_ptr = right._ptr;
58+
right._ptr = NULL;
59+
return *this;
60+
}
61+
62+
63+
T& operator*() {
64+
return *_ptr;
65+
}
66+
67+
T& operator->() {
68+
return _ptr;
69+
}
70+
71+
~SmartPointer(){
72+
cout << "Destructor->" << endl;
73+
delete _ptr;
74+
}
75+
76+
};
77+
78+
class Rectangle {
79+
int length;
80+
int breadth;
81+
82+
public:
83+
Rectangle() = default;
84+
Rectangle(int l, int b){
85+
length = l;
86+
breadth = b;
87+
}
88+
89+
int getArea(){
90+
return length * breadth;
91+
}
92+
93+
94+
};
95+
96+
97+
98+
int main(){
99+
100+
101+
{
102+
/* auto ptrEnt = new Entity(10);
103+
auto_ptr<Entity>ptr(ptrEnt);
104+
(*ptr).doSomething();
105+
*/
106+
//ptr.reset();
107+
//ptr.relase();
108+
109+
110+
//auto_ptr<Entity>ptr1(new Entity(10));//ekvivalenti-> int* ptr
111+
112+
/*auto_ptr<int>ptr1(new int(42));
113+
auto_ptr<int>ptr2 = ptr1;
114+
115+
116+
Entity* ptr = ptr1.get();
117+
(*ptr).doSomething();*/
118+
119+
/* int* ptr1 = new int(42);
120+
int* ptr2 = ptr1;
121+
122+
delete ptr1;*/
123+
124+
/* Entity e;
125+
foo(e);*/
126+
}
127+
128+
{
129+
130+
//unique_ptr<Rectangle> P1(new Rectangle(20, 5));
131+
//cout << P1->getArea() << endl;
132+
133+
//// unique_ptr<Rectangle> P2(P1);
134+
//unique_ptr<Rectangle> P2;
135+
//
136+
//P2 = move(P1);
137+
//cout << P2->getArea() << endl;
138+
139+
140+
}
141+
142+
{
143+
shared_ptr<Rectangle> P1(new Rectangle(20, 5)); // create shared //ptr P1
144+
145+
cout << P1->getArea() << endl;
146+
147+
shared_ptr<Rectangle> P2;
148+
P2 = P1;
149+
150+
151+
cout << P2->getArea() << endl;
152+
153+
154+
cout << P1->getArea() << endl;
155+
156+
157+
cout << P1.use_count() << endl;
158+
159+
}
160+
161+
162+
163+
}
164+

Debtor 2/Debtor 2.vcxproj Smart Pointer/Smart Pointer.vcxproj

+3-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<PropertyGroup Label="Globals">
2222
<VCProjectVersion>16.0</VCProjectVersion>
2323
<Keyword>Win32Proj</Keyword>
24-
<ProjectGuid>{de5562ea-e29d-42d4-b843-60c72d309b48}</ProjectGuid>
25-
<RootNamespace>Debtor2</RootNamespace>
24+
<ProjectGuid>{c404e06f-d2f8-4b63-a717-d4ee884da39e}</ProjectGuid>
25+
<RootNamespace>SmartPointer</RootNamespace>
2626
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
2727
</PropertyGroup>
2828
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
@@ -127,10 +127,7 @@
127127
</Link>
128128
</ItemDefinitionGroup>
129129
<ItemGroup>
130-
<ClCompile Include="Debtor 2.cpp" />
131-
</ItemGroup>
132-
<ItemGroup>
133-
<ClInclude Include="Debtor.h" />
130+
<ClCompile Include="Smart Pointer.cpp" />
134131
</ItemGroup>
135132
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
136133
<ImportGroup Label="ExtensionTargets">

Debtor 2/Debtor 2.vcxproj.filters Smart Pointer/Smart Pointer.vcxproj.filters

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@
1515
</Filter>
1616
</ItemGroup>
1717
<ItemGroup>
18-
<ClCompile Include="Debtor 2.cpp">
18+
<ClCompile Include="Smart Pointer.cpp">
1919
<Filter>Kaynak Dosyalar</Filter>
2020
</ClCompile>
2121
</ItemGroup>
22-
<ItemGroup>
23-
<ClInclude Include="Debtor.h">
24-
<Filter>Kaynak Dosyalar</Filter>
25-
</ClInclude>
26-
</ItemGroup>
2722
</Project>
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
<ProjectOutputs>
4+
<ProjectOutput>
5+
<FullPath>C:\Users\LEGION\source\repos\Smart Pointer\x64\Debug\Smart Pointer.exe</FullPath>
6+
</ProjectOutput>
7+
</ProjectOutputs>
8+
<ContentFiles />
9+
<SatelliteDlls />
10+
<NonRecipeFileRefs />
11+
</Project>
918 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
 Smart Pointer.cpp
2+
Smart Pointer.vcxproj -> C:\Users\LEGION\source\repos\Smart Pointer\x64\Debug\Smart Pointer.exe
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.32.31326:TargetPlatformVersion=10.0.19041.0:
2+
Debug|x64|C:\Users\LEGION\source\repos\Smart Pointer\|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x64\Debug\\_IsIncrementalBuild

Smart Pointer/x64/Debug/vc143.idb

163 KB
Binary file not shown.

Smart Pointer/x64/Debug/vc143.pdb

404 KB
Binary file not shown.

0 commit comments

Comments
 (0)