#include <iostream>
using namespace std;
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
#include <string>
//c++引用方式传递
/*void myswap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}*/
/*int myswap(int &a,int &b)//引用c++
{
int temp=a;
a=b;
b=temp;
}*/
/*int &test02()
{
static int a=10;//局部变量存放在栈区
return a;
}*/
/*void func(int &ref)
{
ref=100;
}*/
void show(int &val)
{
val=1000;
cout<<val;
}
int main()
{
/*//引用的基本语法
int a=10;
//创建引用
int &b=a;
//别名和原名是操作的同一块内存;
cout<<a<<endl;
cout<<b<<endl;
b=100;
cout<<a<<endl;
cout<<b<<endl;*/
//1.引用必须初始化
//2.引用在初始化后,不可以改变
/*int a=10;
int &b=a;
int c=20;
b=20;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c;*/
/*int a=10;
int b=20;
myswap(a,b);
cout<<a<<endl;
cout<<b;*/
//不能返回局部遍历引用 ,因为第一次可以输出成功,但第二次就不可以了,因为内存已经被释放掉了。
/*int &ref=test02();//这是别名。
cout<<"ref="<<ref<<endl;
cout<<"ref="<<ref<<endl;
test02()=1000;//如果函数的返回值是引用,
cout<<"ref="<<ref<<endl;
cout<<"ref="<<ref<<endl;*/
/*int a=10;
int &ref=a;
ref=20;
cout<<"a="<<a<<endl;
cout<<"ref="<<ref;
//引用的本质就是一个指针常量:*/
//常量引用,用来修饰形参防止误操作
//int a=10;
//int &ref=a;//引用必须引用一块合理的内存空间
//int const int &ref=10;
//加上const后,编译器讲代码修改为 int temp=10;const int &ref=temp;
//加入const只读不改.
int a=100;
show(a);
cout<<a;
system("pause");
return 0;
}