forked from selfboot/CS_Offer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathC++_LR_Value.cpp
47 lines (38 loc) · 875 Bytes
/
C++_LR_Value.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
/*
* @Author: [email protected]
* @Last Modified time: 2016-08-31 18:57:14
*/
#include <iostream>
using namespace std;
class cs
{
public:
cs(int i): data(i) { cout << "cs(" << i <<") constructor!" << endl; }
~cs() { cout << "cs destructor,i(" << data << ")" << endl; }
cs& operator=(const cs& other)
{
data = other.data;
cout << "cs operator=()" << endl;
return *this;
}
int get_i() const { return data; }
void change(int i) { data = i; }
private:
int data;
};
cs get_cs()
{
static int i = 0;
return cs(i++);
}
int main()
{
// 合法
(get_cs() = cs(2)).change(323);
get_cs() = cs(2);// operator=()
get_cs().change(32);
// 右值只能被 const 引用
const cs& ref = get_cs();
cout << "Here last cs object get from get_cs has not been destructed.\n";
return 0;
}