-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebug_ctor_dtor.cpp
52 lines (41 loc) · 1.33 KB
/
debug_ctor_dtor.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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// simple test class
struct X {
int val;
void out(const string& s, int nv) {
cerr << this << "->" << s << ": " << val << " (" << nv << ")\n";
}
X() { out("X()", 0); val = 0; } // default constructor
X(int v) { val = v; out("X(int)", v); }
X(const X& x) { val = x.val; out("X(X&)", x.val); } // copy constructor
X& operator=(const X& x) // copy assignment
{ out("X::operator=(X&)", x.val); val = x.val; return *this; }
~X() { out("~X()", 0); } // destructor
};
X glob(2); // a global variable
X copy(X x) { return x; }
X copy2(X x) { X xx = x; return xx; }
X& ref_to(X& x) { return x; }
X* make(int v) { X x(v); return new X(v); }
struct XX { X a; X b; };
int main() {
X loc(4); // local variable
X loc2(loc); // copy construction
loc = X(5); // copy assignment
loc2 = copy(loc); // call by value and return
loc2 = copy2(loc);
X loc3(6);
X& r = ref_to(loc); // call by reference and return
delete make(7);
delete make(8);
vector<X> v(4); // default values
XX loc4;
X* p = new X(9); // an X on the free store
delete p;
X* pp = new X[5]; // an array of Xs on the free store
delete[] pp;
return 0;
}