-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy.cpp
More file actions
60 lines (49 loc) · 1.03 KB
/
copy.cpp
File metadata and controls
60 lines (49 loc) · 1.03 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
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <sequtils.h>
using namespace std;
class Test {
private :
int value;
public :
Test(int v) : value(v) { cout << "calling constructor with " << v << endl; }
~Test() { cout << "destroying " << value << endl; }
Test(const Test& t) {
cout << "calling copy constructor obj " << this << " from " << &t << endl;
if (this != &t) {
value = t.value;
}
}
Test& operator=(const Test& t)
{
if (this != &t) {
value = t.value;
}
return *this;
}
int getValue() const { return value; }
void set(int v) { value = v; };
};
ostream& operator<<(ostream& os, const Test& t)
{
os << "[ref " << &t << " value " << t.getValue() << "] " << endl;
return os;
}
int main(int argc, char *argv[])
{
list<Test> a;
list<Test> b;
Test v(0);
for(int i = 0; i < 10; ++i) {
v.set(i);
a.push_back(v);
}
cout << endl;
cout << "before copying " << endl;
b = a;
print_seq(a);
print_seq(b);
return 0;
}