-
Notifications
You must be signed in to change notification settings - Fork 1
/
13.cpp
53 lines (45 loc) · 912 Bytes
/
13.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 <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
struct X
{
X() { cout << "X()" << endl;}
X(const X&) { cout << "X(const X&)" << endl; }
X& operator=(const X&) { cout << "X& operator=(const X&)" << endl; }
~X() { cout << "~X()" << endl; }
};
/*
int main(){
// X x1;
X *x1 = new X;
X x2(*x1);
vector<X> vec;
// vec.push_back(*x1);
// vec.push_back(x2);
delete x1;
// delete x2;
return 0;
}
*/
void f(const X &rx, X x) // reference is not the copy-assignment
{
cout << "f start" << endl;
std::vector<X> vec;
vec.reserve(2);
vec.push_back(rx);
cout << " rx " << endl;
vec.push_back(x);
cout << " x " << endl; // the destrucotor is invoked 3 times.
}
int main()
{
X *px = new X;
cout << "new X" << endl;
f(*px, *px);
cout << "f end" << endl;
delete px;
cout << "delete px" << endl;
return 0;
}