-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path06_04.cpp
53 lines (44 loc) · 1.09 KB
/
06_04.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
#include <bits/stdc++.h>
using namespace std;
class MyNumber {
private:
int *val1;
int val2;
public:
MyNumber(int x = 3, int y = 5) {
cout << "Normal ructor\n";
val1 = new int;
*val1 = x;
val2 = y;
}
MyNumber( MyNumber &another) {
cout << "Copy ructor\n";
val1 = new int;
*val1 = *another.val1;
val2 = another.val2;
}
~MyNumber() {
delete val1;
}
void PrintValueAndAddress() {
cout << "val1: " << *val1 << " " << val1 << "\n";
cout << "val2: " << val2 << " " << &val2 << "\n\n";
}
};
void play1(MyNumber a) {}
void play2(MyNumber &a) {}
void play3( MyNumber &a) {}
MyNumber play4() {
MyNumber x(1, 1);
return x; // Returned object is temporary
}
int main() {
play1(MyNumber());
// play2(MyNumber()); // cannot be bound to a non- reference
play3(MyNumber());
// play2(play4()); // cannot be bound to a non- reference
// Notice: Sometimes the copy ructor won't be called!
// Due to C++ return value optimization (RVO)
// It eliminates the temporary object created to hold a function's return value
return 0;
}