forked from zqmath1994/CS_Offer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass_ConDesAssign.cpp
69 lines (56 loc) · 1.72 KB
/
Class_ConDesAssign.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
/*
* @Author: [email protected]
* @Last Modified time: 2016-04-17 10:57:53
*/
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "1" << endl;
}
~A(){
cout << "2" << endl;
}
A(const A&b){
cout << "3" << endl;
}
A& operator= (const A& b){
cout << "4" << endl;
return *this;
}
};
void f(A a){ // 3, Copy Constructor. Create a temp object as parameter.
} // 2, Destructors. Delete the temp object.
A f_2(A a){ // 3, Copy Constructor. Create a temp object as parameter.
return a; // 3, Copy Constructor. Create a temp object as return value.
} // 2, Destructors. Delete the parameter.
void g(A &a){ // None.
} // None.
A & g_2(A &a){
return a;
}
void h(A *a){ // None.
} // None.
int main(void)
{
A a; // 1, Constructor
f(a); // 3 2
g(a); // None
A c = f_2(a); // 3 3 2
// f_2(a); // 3 3 2 2. The object returned is destructed here because of no use.
g_2(a); // None
A b=a; // 3, Copy Constructor. Same with A b(a)
b = a; // 4, Assignment operator. b is already existing.
A *d; // None, just a A * point.
d = new A(); // 1, Constructor. Then d point to the temp A object.
A *e = d; // None. new A * pointer point to existing A object.
h(d); // None
delete d; // 2, Destructor. Delete the A object in heap.
d = NULL;
// delete e; // d, e point to the same A object in heap. Here e is NULL.
return 0;
// 2, Destructor a
// 2, Destructor c
// 2, Destructor b
}