-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeSnap-PointersAndReferences.cpp.html
169 lines (152 loc) · 5.98 KB
/
CodeSnap-PointersAndReferences.cpp.html
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
<!----------------------------------------------------------------------------
CodeSnap-PointersAndReferences.cpp.htm
Published 19 Mar 2017
Jim Fawcett, CSE687 : Object Oriented Design, Summer 2017
Note:
- Markup characters in the text part, enclosed in <pre>...</pre> have to be
replaced with escape sequences, e.g., < becomes < and > becomes >
- Be careful that you don't replace genuine markup characters with escape
sequences, e.g., everything outside of the <pre>...</pre> section.
----------------------------------------------------------------------------->
<html>
<head>
<script src="js/ScriptsUtilities.js"></script>
<script src="js/ScriptsTemplate.js"></script>
<script src="js/ScriptsKeyboard.js"></script>
<script src="js/ScriptsMenuCpp.js"></script>
<link rel="stylesheet" href="css/StylesTemplate.css" />
<link rel="stylesheet" href="css/StylesMenu.css" />
<style>
h3 {
font-weight: normal;
}
</style>
</head>
<body id="github" onload="initializeMenu()">
<nav>
<div id="navbar"></div>
</nav>
<a id="Next" href="CodeSnap-PointersAndReferences.txt.html">N</a>
<a id="Prev" href="CodeSnap-PointersAndReferences.txt.html">P</a>
<navKeys-Container>
<nav-Key id="sKey" onclick="toggleSwipeEvents()">S</nav-Key>
<nav-Key id="rKey" onclick="location.reload()">R</nav-Key>
<nav-Key id="tKey" onclick="scrollPageTop()">T</nav-Key>
<nav-Key id="bKey" onclick="scrollPageBottom()">B</nav-Key>
<nav-Key id="hKey" onclick="helpWin()">H</nav-Key>
<nav-Key id="pKey" onclick="loadPrev()">P</nav-Key>
<nav-Key id="nKey" onclick="loadNext()">N</nav-Key>
</navKeys-Container>
<h3>
<a href="CodeSnap-PointersAndReferences.cpp.html">PointersAndReferences.cpp</a>,
<a href="CodeSnap-PointersAndReferences.txt.html">PointersAndReferences.txt</a>,
<a href="CppBasicDemos.html">Code folder</a>
</h3>
<div class="indent">
This demo illustrates how pointers and references are used in C++ code.
We've demonstrated std::unique_ptr and std::shared_ptr as well as native
pointers.
</div>
<hr />
<h3>PointersAndReferences.cpp</h3>
<pre class="codeSnap">
/////////////////////////////////////////////////////////////////
// PointersAndReferences.cpp - demonstration //
// //
// Jim Fawcett, CSE687 - Object Oriented Design, Summer 2017 //
/////////////////////////////////////////////////////////////////
/*
* This package demonstrates access to variables defined in the
* static, stack, and native heap memory regions.
* - Variables declared as static reside in static memory and
* live for the duration of the program.
* - Non-static variables reside in the stack frame of their local
* scope and are destroyed when the thread of execution leaves
* that scope.
* - Variables created with new reside in the native heap and are
* destroyed when delete is called on their addresses.
*/
#include <string>
#include <iostream>
#include <memory>
#include "../Utilities/Utilities.h"
///////////////////////////////////////////////////////////
// Demonstration class
class Demo
{
public:
using Message = std::string;
Demo(const Message& msg) : msg_(msg) {}
~Demo()
{
putLine("destroying " + this->message());
}
Message& message()
{
return msg_;
}
Message countedMessage()
{
static size_t count = 0; // executed only once on initial call,
// creating initialized variable in static memory
++count;
Message temp = "#" + Convert<size_t>::toString(count) + " " + msg_;
return temp;
}
private:
Message msg_;
};
///////////////////////////////////////////////////////////
// Demonstration
Cosmetic cosmetic("note order of destruction");
int main()
{
putTitle("Demonstrating Pointers and References", '=');
putLine();
putTitle("declare instance of custom class Demo");
Demo demo("demo"); // created in main's stack frame
putLine(demo.countedMessage());
putLine();
putTitle("declare and use pointer to demo");
Demo* pDemo = &demo; // pointer to instance in stack memory so don't delete
putLine(pDemo->countedMessage());
putLine();
putTitle("declare and use reference to demo");
Demo& rDemo = demo;
putLine(rDemo.countedMessage());
putLine();
putTitle("declare and use pointer to Demo instance on heap");
pDemo = new Demo("demo on heap"); // pointer to instance in heap, so delete
putLine(pDemo->countedMessage());
delete pDemo;
putLine();
putTitle("declare and use reference to Demo instance on heap");
Demo& rHeapDemo = *new Demo("demo on heap"); // reference to instance in heap, so delete
putLine(rHeapDemo.countedMessage());
delete &rHeapDemo;
putLine();
putTitle("you can declare and use reference to reference to a temporary");
Demo&& rrDemo = Demo("another Demo instance"); // in stack memory so don't delete
putLine(rrDemo.countedMessage());
putLine();
putTitle("declare a unique_ptr to refer to heap allocation with automatic destruction");
std::unique_ptr<Demo> uPtr(new Demo("referenced by std::unique_ptr")); // don't call delete, uPtr will
putLine(uPtr->countedMessage());
putLine();
putTitle("declare a shared_ptr sharing a reference counted heap allocation");
std::shared_ptr<Demo> sPtr1(new Demo("demo referenced by std::shared_ptr sPtr1")); // don't call delete
putLine(sPtr1->countedMessage());
putLine();
putTitle("declare a shared_ptr sharing a reference counted heap allocation");
std::shared_ptr<Demo> sPtr2(sPtr1); // this is how you share an allocation
sPtr2->message() = "demo referenced by std::shared_ptr sPtr2";
putLine(sPtr2->countedMessage());
putLine();
putLine("---- end of main ----");
}</pre>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<info-bar></info-bar>
</body>
</html>