Skip to content

Commit d745937

Browse files
committed
update c++
1 parent 8687251 commit d745937

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Diff for: Coding/C++_LR_Value.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-08-31 18:57:14
4+
*/
5+
6+
#include <iostream>
7+
using namespace std;
8+
9+
class cs
10+
{
11+
public:
12+
cs(int i): data(i) { cout << "cs(" << i <<") constructor!" << endl; }
13+
~cs() { cout << "cs destructor,i(" << data << ")" << endl; }
14+
15+
cs& operator=(const cs& other)
16+
{
17+
data = other.data;
18+
cout << "cs operator=()" << endl;
19+
return *this;
20+
}
21+
22+
int get_i() const { return data; }
23+
void change(int i) { data = i; }
24+
25+
private:
26+
int data;
27+
};
28+
29+
cs get_cs()
30+
{
31+
static int i = 0;
32+
return cs(i++);
33+
}
34+
35+
int main()
36+
{
37+
// 合法
38+
(get_cs() = cs(2)).change(323);
39+
get_cs() = cs(2);// operator=()
40+
get_cs().change(32);
41+
42+
// 右值只能被 const 引用
43+
const cs& ref = get_cs();
44+
cout << "Here last cs object get from get_cs has not been destructed.\n";
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)