Skip to content

Commit

Permalink
update c++
Browse files Browse the repository at this point in the history
  • Loading branch information
selfboot committed Aug 31, 2016
1 parent 8687251 commit d745937
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Coding/C++_LR_Value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

0 comments on commit d745937

Please sign in to comment.