forked from selfboot/CS_Offer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |