Created
December 28, 2016 14:41
-
-
Save zhpmatrix/015c20380a00828cddf635a71fa4dcdf to your computer and use it in GitHub Desktop.
关于C++中各种返回值的形式和右值引用结合
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
#include <iostream> | |
using namespace std; | |
void swap(int* a,int& b){ | |
int t = *a; | |
*a = b; | |
b = t; | |
} | |
void swap(int& a,int& b){ | |
int t = move(a); | |
a = move(b); | |
b = move(t); | |
} | |
int& add(int a,int b,int& result){ | |
result = a + b; | |
return result; | |
} | |
int add(int a,int b){ | |
int result = a + b; | |
return move(result); | |
} | |
int& add(int a,int* result){ | |
*result += a; | |
return *result; | |
} | |
//const int& add(int a,int* result){ | |
// *result += a; | |
// return *result; | |
//} | |
int main(){ | |
int a = 3,b = 4; | |
int result_1 = 0; | |
int result_2 = 0; | |
result_2 = add(a,b); | |
cout << result_2 << endl; | |
add(a,b,result_1); | |
cout << result_1 << endl; | |
add(3,&result_1)++; | |
cout << result_1 << endl; | |
swap(a,b); | |
cout << a << "," << b << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment