Skip to content

Instantly share code, notes, and snippets.

@zhpmatrix
Created December 28, 2016 14:41
Show Gist options
  • Save zhpmatrix/015c20380a00828cddf635a71fa4dcdf to your computer and use it in GitHub Desktop.
Save zhpmatrix/015c20380a00828cddf635a71fa4dcdf to your computer and use it in GitHub Desktop.
关于C++中各种返回值的形式和右值引用结合
#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