-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
111 lines (89 loc) · 2.35 KB
/
main.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
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
#include <iostream>
using namespace std;
class A {
int data;
public:
A() { cout << "A()" << endl; }
~A() { cout << "~A()" << endl; }
A(const A& a) { SetData(a.GetData()); cout << "A(A&)" << endl; }
A& operator=(const A& other) {
cout << "A& operator=(const A&)" << endl;
if (this == &other) return *this;
SetData(other.GetData());
return *this;
}
A(A&& other) noexcept { cout << "A(A&&)" << endl; }
A& operator=(A&& other) noexcept {
cout << "A& operator=(const A&&) noexcept" << endl;
if (this == &other) return *this;
return *this;
}
void SetData(int val) { data = val; }
int GetData() const { return data; }
};
class B {
public:
A GetA() { return A(); }
// Все что после return можно считать уже там.
// Здесь не будет копирования результата.
A GetA_() {
A a;
a.SetData(100500);
cout << "|" << endl;
return a;
}
};
// The called function has access to the memory the return value will occupy,
// even though that memory is not "in scope" when the copy is being made,
// it's still available.
// (c) https://stackoverflow.com/a/665794
// Copy construtor was called only once on the returning object,
// when assigning the returned object to another outer scope object,
// assignment operator is called, not the copy constructor.
// (c) https://stackoverflow.com/questions/665781/copy-constructor-in-c-is-called-when-object-is-returned-from-a-function#comment20804168_665916
void test1() {
B b;
b.GetA();
// A()
// ~A()
// Объект сразу уничтожается, т.к. ничему не назначен.
cout << "end;" << endl;
// end;
}
void test2() {
B b;
A a = b.GetA();
// A()
cout << "end;" << endl;
// end;
// ~A()
}
void test3() {
B b;
A a = b.GetA_();
// A()
// |
cout << a.GetData() << endl;
// 100500
A a2;
// A()
a2 = a;
// A& operator=(const A&)
cout << "end;" << endl;
// end;
// ~A()
// ~A()
}
int main() {
cout << "test1:" << endl;
test1();
cout << endl;
cout << "test2:" << endl;
test2();
cout << endl;
cout << "test3:" << endl;
test3();
cout << endl;
cout << "end." << endl;
return 0;
}