forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathcpptest.cpp
185 lines (160 loc) · 6.55 KB
/
cpptest.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
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
/*
* This program is designed to show some nuances of C++ syntax,
* reference usage, and the various methods in a class (copy
* constructor, operator=(), etc.).
*
* The addresses you get when running this program will most likley
* differ; that is expected. In fact, the addresses will differ upon
* successive executions of the program. If you are compiling this on
* a 64-bit machine, you may want to use the -m32 flag to g++ to
* compile it in 32-bit mode -- this keeps the pointers to 32 bits (as
* opposed to 64 bits).
*
* Copyright (c) 2012 by Aaron Bloomfield (aaron@virginia.edu). All
* rights reesrved.
*/
#include <iostream>
using namespace std;
//--------------------------------------------------
// this would be in test.h
class test {
static int idcount;
const int id;
int value;
public:
test();
test(int v);
test(const test& x);
~test();
test& operator=(const test& other);
friend ostream& operator<<(ostream& out, const test& f);
};
//--------------------------------------------------
// this would be in test.cpp
int test::idcount = 0;
test::test() : id (idcount++), value(0) {
cout << "calling test(); object created is " << *this
<< "; address is " << this << endl;
}
test::test(int v) : id (idcount++), value(v) {
cout << "calling test(" << v << "); object created is " << *this
<< "; address is " << this << endl;
}
test::test(const test& x) : id(x.id), value(x.value) {
cout << "calling test(const test&) on " << *this << "; address is " << this << endl;
}
test::~test() {
cout << "calling ~test() on " << *this << endl;
}
test& test::operator=(const test& other) {
cout << "calling operator=(" << other << ")" << endl;
test *tmp = new test(other);
return *tmp;
}
ostream& operator<<(ostream& out, const test& f) {
out << "test[id=" << f.id << ",v=" << f.value << "]";
return out;
}
//--------------------------------------------------
// this would be in main.cpp
test bar(test param) {
return test(10);
}
int main() {
// this does NOT create a test object; instead, it creates a function
// called a() that has no body (and thus C++ defaults it to return
// 1). Note that there is no output statement from the default
// constructor.
test a();
cout << "attempted to create a: " << a << endl;
// output:
// attempted to create a: 1
// this does create a test object, calling the default constructor.
// Since putting parenthesis in there for creating an object and
// calling the default constructor has another meaning in C++
// (specifically, creating a function), we have to omit the
// parenthesis.
cout << "----------------------------------------" << endl;
test aa;
cout << "created aa: " << aa << endl;
// output:
// ----------------------------------------
// calling test(); object created is test[id=0,v=0]; address is 0xff852a50
// created aa: test[id=0,v=0]
// this creates a test object, calling the specific constructor that
// takes in a single int value
cout << "----------------------------------------" << endl;
test b(1);
cout << "created b: " << b << endl;
// output:
// ----------------------------------------
// calling test(1); object created is test[id=1,v=1]; address is 0xff852a48
// created b: test[id=1,v=1]
// this creates two test objects via pointers and new
cout << "----------------------------------------" << endl;
test *c = new test(2);
cout << "created *c: " << *c << " at " << c << endl;
test *d = new test;
cout << "created *d: " << *d << " at " << d << endl;
// output:
// ----------------------------------------
// calling test(2); object created is test[id=2,v=2]; address is 0xa009008
// created *c: test[id=2,v=2] at 0xa009008
// calling test(); object created is test[id=3,v=0]; address is 0xa009018
// created *d: test[id=3,v=0] at 0xa009018
// subroutine invocation. The copy constructor is invoked when the
// actual parameter is copied into the formal parameter. The
// subroutine then creates test[id=4,v=10]. The parameter that was
// copied into the subroutine (via the copy constructor) is then
// destructed upon termination of the subroutine.
cout << "----------------------------------------" << endl;
cout << "about to invoke subroutine..." << endl;
test e = bar(*c);
cout << "finished invoking subroutine..." << endl;
// output:
// ----------------------------------------
// about to invoke subroutine...
// calling test(const test&) on test[id=2,v=2]; address is 0xff852a38
// calling test(10); object created is test[id=4,v=10]; address is 0xff852a40
// calling ~test() on test[id=2,v=2]
// finished invoking subroutine...
// because this assignment happens in the same statement as the
// declaration, it invokes the copy constructor
cout << "----------------------------------------" << endl;
test f = b;
// output:
// ----------------------------------------
// calling test(&test) on test[id=1,v=1]; address is 0xff852a30
// we are only deleting one of the dynamically created test objects
cout << "----------------------------------------" << endl;
cout << "about to delete a test object..." << endl;
delete c;
// output:
// ----------------------------------------
// about to delete a test object...
// calling ~test() on test[id=2,v=2]
// because this assignment does NOT happen in the same statement as
// the declaration, the operator=() subroutine is called.
cout << "----------------------------------------" << endl;
cout << "assignment..." << endl;
aa = b;
// output:
// ----------------------------------------
// assignment...
// calling operator=(test[id=1,v=1])
// calling test(&test) on test[id=1,v=1]; address is 0xa009008
// upon termination of the main() function, all statically created
// test objects are deallocated.
cout << "----------------------------------------" << endl;
cout << "about to leave main..." << endl;
// output:
// ----------------------------------------
// about to leave main...
// calling ~test() on test[id=1,v=1]
// calling ~test() on test[id=4,v=10]
// calling ~test() on test[id=1,v=1]
// calling ~test() on test[id=0,v=0]
// Note that d was created via dynamic memory allocation, so it is
// not deallcoated by the program. The operating system will clean
// up the used memory, but it does not call the destructor on d.
}