引用就是给变量起别名。
1.基本操作:
数据类型 & 别名 = 变量名;
代码如下:
//a,b 同步修改。
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
int main()
{
int a = 10;
int& b = a;
cout << a << " " << b << endl;
b = 29;
cout << a << " " << b << endl;
system("pause");
return 0;
}
2.引用的注意事项:
1.引用必须初始化 int &b;是错误的
2.引用不可更改。
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
int main()
{
int a = 10;
int& b = a;
//1.引用必须初始化 int &b;是错误的/
//2.引用不可更改。
int c = 90;
b = c;//此处是赋值操作,不是修改引用的操作,a,b,c被修改。
cout << a << " " << b <<" "<<c<<endl;
system("pause");
return 0;
}
3.引用做函数参数。
之前学习过,想用形参修饰实参就要用到地址传递,值传递不可改变实参。
今天学习一下引用修饰函数参数,也可以改变实参。
优点:可以简化指针修改实参。
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
void myswap1(int a,int b)
{
int t= a;
a = b;
b = t;
}
void myswap2(int* a, int* b)
{
int t= *a;
*a = *b;
*b = t;
}
void myswap3(int& a, int& b)
{
int t = a;
a = b;
b = t;
}
int main()
{
int a = 10;
int b = 39;
myswap1(a, b);
cout <<"值传递: "<< a << " " << b <<" "<<endl;
myswap2(&a, &b);
cout << "地址传递: " << a << " " << b << " " << endl;
myswap3(a, b);
cout << "引用做函数形参: " << a << " " << b << " " << endl;
system("pause");
return 0;
}
4.引用函数作返回值。
a.不能返回局部变量的引用
b.如果函数的返回值是“引用”,那么这个函数的调用可以做左值。
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
int& test1()
{
//返回局部变量引用(栈区)
int a=10;
return a;
}
int& test2()
{
//返回全局(变量)变量引用(全局区)
static int a = 10;
return a;
}
int main()
{
int& ref1 = test1();
//不能返回局部变量的引用
cout << ref1 << endl;//第一次返回正确,因为编译器做了保留
cout << ref1 << endl;//第二次返回错误,因为内存已经释放
int& ref2 = test2();
cout << ref2 << endl;
cout << ref2 << endl;
test2() = 1000;//test02是原名,ref2是别名。
//如果函数的返回值是“引用”,那么这个函数的调用可以做左值。
cout << ref2 << endl;
cout << ref2 << endl;
system("pause");
return 0;
}
5.引用的本质
本质是指针常量。
int& ref=a;
__//发现是引用,转换为int const ref=&a;
ref = 1000;//自动执行ref=1000;
是个指针常量,指针常量的指向不可修改,而也证明了,引用一旦声明便不可修改。
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
void func(int& ref)
//发现是引用,转换为int *const ref=&a;
{
ref = 1000;//自动执行*ref=1000;
}
int main()
{
int a = 10;
int& ref = a;//发现是引用,自动转换为int* const ref=&a;
//是个指针常量,指针常量的指向不可修改,而也证明了,引用一旦声明便不可修改。
ref = 20;//自动执行*ref=20;
cout << a << " " << ref << endl;
func(a);
cout << a << " " << ref << endl;
system("pause");
return 0;
}
6.常量引用:
A.引用的使用场景,通常用来修饰形参。
B. //int& ref = 12;
//引用必须是一段合法的内存的空间,堆栈那些东西,因此会报错。
C.const int& ref = 12;
//此处编译器自动进行int temp=12; int& ref = temp;
D.常量引用不可修改
void showref(const int& ref)
{
//ref = 1023;,此处会把a也改了,因此加上const
//用来防止误操作。
cout << ref << " " << endl;
}
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
//引用的使用场景,通常用来修饰形参,
void showref(const int& ref)
{
//ref = 1023;,此处会把a也改了,因此加上const
//用来防止误操作。
cout << ref << " " << endl;
}
int main()
{
//int& ref = 12;
//引用必须是一段合法的内存的空间,堆栈那些东西,因此会报错。
const int& ref = 12;
//此处编译器自动进行int temp=12; int& ref = temp;
// ref = 123;//常量引用不可修改
int a = 1;
showref(a);
cout << a;
system("pause");
return 0;
}