forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestructors.cpp
More file actions
112 lines (90 loc) · 3.33 KB
/
Copy pathdestructors.cpp
File metadata and controls
112 lines (90 loc) · 3.33 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>
using namespace std;
class MyClass {
public:
int * a;
private:
int * b;
int c;
string name;
public:
// Constructor
MyClass(int x=0, const string n="") : name(n) {
static int counter = 1; // Allocated in static memory
a = new int; // Allocated on the heap
*a = x;
b = new int[*a]; // Allocated on the heap
for (int ii=0;ii<*a;ii++) {
b[ii] = ii * *a;
}
c = counter;
cout << " Constructor " << name << " " << *a << " {" << a << "} : "
<< "b=" << b << " c=" << c << endl;
counter++;
}
// Copy Constructor
MyClass(const MyClass & original) {
a = new int; // Allocated on the heap
*a = *(original.a);
b = new int[*a]; // Allocated on the heap
for (int ii=0;ii<*a;ii++) {
b[ii] = original.b[ii];
}
c = original.c + 6000;
cout << " Copy ctor " << name << " " << *a << " {" << a << "} : "
<< "b=" << b << " c=" << c << endl;
}
// Destructor
~MyClass() {
cout << " Destructor " << name << " " << *a << " {" << a << "} : "
<< "b=" << b << " c=" << c << endl;
delete a;
a = nullptr;
delete[] b; // b points to an array, so use delete[]
b = nullptr;
}
void print() const;
};
void MyClass::print() const {
cout << " Print: " << name << " " << *a << " {" << a << "} : "
<< "b=" << b << " c=" << c << endl;
}
void func(MyClass funParam) {
cout << " FUNC: ++++++++++++ start func ++++++++++++" << endl;
cout << " FUNC: begin declare func1" << endl;
MyClass func1(3, "func1");
cout << " FUNC: end declare func1" << endl;
cout << " FUNC: begin print func1" << endl;
func1.print();
cout << " FUNC: end print func1" << endl;
cout << " FUNC: begin print funParam" << endl;
funParam.print();
cout << " FUNC: end print funParam" << endl;
cout << " FUNC: ++++++++++++ end func ++++++++++++" << endl;
}
int main() {
cout << "MAIN: begin" << endl;
cout << "MAIN: begin declare main1 (object)" << endl;
MyClass main1(1, "main1");
cout << "MAIN: end declare main1 (object)" << endl;
cout << "MAIN: begin declare main2ptr (pointer)" << endl;
MyClass * main2ptr;
cout << "MAIN: end declare main2ptr (pointer)" << endl;
cout << "MAIN: begin declare main2ptr (pointer)" << endl;
main2ptr = new MyClass(2, "main2ptr");
cout << "MAIN: end declare main2ptr (pointer)" << endl;
cout << "MAIN: print main1 (&main1 = " << &main1 << ")" << endl;
main1.print();
cout << "MAIN: print main2ptr (&main2ptr = " << &main2ptr << ", main2ptr = " << main2ptr << ")" << endl;
main2ptr->print();
cout << "MAIN: begin call to func(main1)" << endl;
func(main1);
cout << "MAIN: end call to func(main1)" << endl;
cout << "MAIN: print main1 (&main1 = " << &main1 << ")" << endl;
main1.print();
cout << "MAIN: begin delete main2ptr" << endl;
delete main2ptr;
cout << "MAIN: end delete main2ptr" << endl;
cout << "MAIN: complete" << endl;
return 0;
}