-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc04091.cpp
More file actions
60 lines (45 loc) · 974 Bytes
/
src04091.cpp
File metadata and controls
60 lines (45 loc) · 974 Bytes
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 "stdafx.h"
#include <iostream>
using namespace std;
class CData {
public:
explicit CData(int param) {
cout << "CData(int)" << endl;
pndata = new int(param);
}
CData(const CData &rhs) {
cout << "CData(const CData &)" << endl;
pndata = new int(*rhs.pndata);
}
~CData() { delete pndata; }
operator int() { return *pndata; }
CData operator+(const CData& rhs) {
cout << "operator+" << endl;
return CData(*pndata + *rhs.pndata);
}
CData& operator=(const CData& rhs) {
cout << "operator=" << endl;
if (this == &rhs) return *this;
delete pndata;
pndata = new int(*rhs.pndata);
return *this;
}
CData& operator=(CData&& rhs) {
cout << "operator= (Move)" << endl;
pndata = rhs.pndata;
rhs.pndata = NULL;
return *this;
}
private:
int *pndata = nullptr;
};
int main() {
CData a(0), b(3), c(4);
cout << "Before" << endl;
a = b + c;
cout << "After" << endl;
cout << a << endl;
a = b;
cout << a << endl;
return 0;
}