五.引用
1.代码
:
#include<iostream>
using namespace std;
void Swap_z(int a,int b)
{
int num = a;
a = b;
b = num;
}
void Swap_d(int* a, int* b)
{
int num = *a;
*a = *b;
*b = num;
}
void Swap_y(int &a, int &b)
{
int num = a;
a = b;
b = num;
}
int& test_j(void)
{
int a = 10;
return a;
}
int& test_t(void)
{
static int a = 2020;
return a;
}
void test_1(int& re)
{
re = 138;
}
int main()
{
int a = 167;
int &b = a;
int c = 22;
cout << "int &b = a; a = " << a << endl;
cout << " b = " << b << endl;
b = 177;
cout << "b = 177; a = " << a << endl;
cout << " b = " << b << endl;
a = 166;
cout << "a = 166; a = " << a << endl;
cout << " b = " << b << endl << endl;
a = 11;
cout << " a = " << a << endl;
cout << " c = " << c << endl;
Swap_z(a,c);
cout << "Swap_z(a,c); a = " << a << endl;
cout << " c = " << c << endl;
Swap_d(&a,&c);
cout << "Swap_d(&a,&c); a = " << a << endl;
cout << " c = " << c << endl;
Swap_y(a,c);
cout << "Swap_d(a,c); a = " << a << endl;
cout << " c = " << c << endl;
cout << " 引用与左值 " << endl;
int& ref = test_j();
cout << "int& ref = " << ref << endl;
cout << "int& ref = " << ref << endl;
int& ref1 = test_t();
cout << "int& ref1 = " << ref1 << endl;
cout << "int& ref1 = " << ref1 << endl;
test_t() = 1001;
cout << "test_t() = 1001; ref1 = " << ref1 << endl;
cout << "test_t() = 1001; ref1 = " << ref1 << endl;
a = 10;
cout << endl << " 引用的本质 " << endl;
int& re = a;
re = 20;
cout << " a = " << a << endl
<< "re = " << re << endl;
cout << endl << " 常量引用 " << endl;
const int re1 = 16;
cout << "re1 = " << re1 << endl;
system("pause");
return 0;
}
2.运行结果
:

