#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#define N 10
void ReferenceSample() {
int a = 10;
int& b = a;
int& c = a;
int& d = b;
a = 20;
b = 30;
int e = 20;
b = c;
}
void ReferenceSwap(int& r1, int& r2) {
int tmp = r1;
r1 = r2;
r2 = tmp;
}
int Add(int a,int b) {
int c = a + b;
return c;
}
int& ReferenceAdd(int a, int b) {
int c = a + b;
return c;
}
int& RefReturn1() {
int* p = (int*)malloc(4);
return *p;
}
int& RefReturn2(int i) {
static int a[N];
return a[i];
}
void RefReturn2Test() {
for (size_t i = 0; i < N; ++i) {
RefReturn2(i) = 10 + i;
}
for (size_t i = 0; i < N; ++i) {
cout << RefReturn2(i) << " ";
}
}
void ReferenceConst(){
const int a = 10;
const int& b = a;
int c = 10;
const int& d = c;
}
int main() {
ReferenceSample();
int x = 0, y = 1;
ReferenceSwap(x, y);
int ret1 = Add(1, 2);
cout << ret1 << endl;
int& ret2 = ReferenceAdd(1, 2);
cout << ret2 << endl;
RefReturn2Test();
return 0;
}
|