作用:给变量起别名
语法:数据类型 &别名 = 原名
#includeusing namespace std; int main() { //引用基本数据类型 //数据类型 &别名 = 原名 int a = 10; int& b = a; cout << "a=" << a << endl; cout << "b=" << b << endl; b = 1000; cout << "a=" << a << endl; cout << "b=" << b << endl; system("pause"); return 0; }
a=10引用注意事项
b=10
a=1000
b=1000
- 引用必须初始化
- 引用在初始化后不可以改变
#includeusing namespace std; int main() { int a = 10; //1、引用必须初始化 //int &b;//错误,必须初始化 int &b = a; //2、引用在初始化后不可以改变 int c = 20; b = c;//赋值操作,而不是更改引用 cout << "a=" << a << endl; cout << "b=" << b << endl; cout << "c=" << c << endl; system("pause"); return 0; }
在引用初始化后,在等于其他的变量就是赋值操作了,而不是更改引用
引用做函数参数作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参
1、值传递:形参不会修饰实参
#includeusing namespace std; //值传递 void swap01(int a, int b) { int temp = a; a = b; b = temp; cout << "swap01 a=" << a << endl; cout << "swap02 b=" << b << endl; } int main() { int a = 10; int b = 20; swap01(a, b); cout << "a=" << a << endl; cout << "b=" << b << endl; system("pause"); return 0; }
swap01 a=20
swap01 b=10
a=10
b=20
2、地址传递:形参会修饰实参
#includeusing namespace std; //地址传递 void swap02(int *a, int *b) { int temp = *a; *a = *b; *b = temp; cout << "swap02 a=" << *a << endl; cout << "swap02 b=" << *b << endl; } int main() { int a = 10; int b = 20; swap02(&a, &b); cout << "a=" << a << endl; cout << "b=" << b << endl; system("pause"); return 0; }
swap02 a=20
swap02 b=10
a=20
b=10
3、引用传递:形参会修饰实参
#includeusing namespace std; //引用传递 void swap03(int &a, int &b) { int temp = a; a = b; b = temp; cout << "swap03 a=" << a << endl; cout << "swap03 b=" << b << endl; } int main() { int a = 10; int b = 20; swap03(a, b); cout << "a=" << a << endl; cout << "b=" << b << endl; system("pause"); return 0; }
swap03 a=20
swap03 b=10
a=20
b=10
总结:通过引用参数产生的效果同地址传递是一样的。引用的语法更清楚简单
引用做函数返回值作用:引用是可以作为函数的返回值存在的
注意:不要返回局部变量引用
示例:返回局部变量引用
#includeusing namespace std; //引用做函数的返回值 //1、不要返回局部变量的引用 int& test01() { int a = 10;// 局部变量存放在四区中的 栈区 return a; } int main() { int& b = test01(); cout << "b=" << b << endl;//a的内存已经释放 system("pause"); return 0; }
b=-858993460
示例:返回静态变量
用法:函数调用作为左边的值
如果函数的返回值是引用,这个函数调用就可以作为左值
#includeusing namespace std; //引用做函数的返回值 //1、不要返回局部变量的引用 int& test01() { static int a = 10;// 静态变量,存放在全局区,全局区的数据在程序结束后释放 return a; } int main() { int& b = test01(); cout << "b=" << b << endl; test01() = 100; cout << "b=" << b << endl; cout << "b=" << b << endl; system("pause"); return 0; }
b=10
b=100
b=100
如果函数的返回值是引用,这个函数调用就可以作为左值
引用的本质本质:引用的本质再C++内部实现是一个指针常量
指针常量:指针指向不可改,指针指向的值可以改
ref是引用,内部自动转换为 *ref
int &ref,内部自动转换为int* const ref = &a
#includeusing namespace std; //发现是引用,转换为int* const ref = &a; void test01(int &ref) { ref = 100;//ref是引用,转换为*ref = 100 } int main() { int a = 10; //自动转换为int* const ref = &a //指针常量是指针指向不可改,也就说明了引用为什么不可更改 int& ref = a; ref = 20;//内部发现ref是引用,自动帮我们转换为 *ref = 20 cout << "a=" << a << endl; cout << "ref=" << ref << endl; test01(a); return 0; }
a=20
ref=20
结论:C++推荐使用引用技术,因为语法方便,引用本质就是指针常量,但是所有的指针操作编译器都帮我们做了
常量引用作用:常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加const修饰形参,防止形参修改实参
#includeusing namespace std; //打印数据函数 void show(const int& val) { //val = 1000;//加入const后函数内将无法赋值 cout << "val = " << val << endl; } int main() { //常量引用 //使用场景:用来修饰形参,防止误操作 //加上const后 编译器将代码修改 int temp = 10;const int &ref = temp const int& ref = 10;//引用必须引一块合法的内存空间 //ref = 20;// 加入const之后变为只读,不可修改 int a = 100; show(a); cout << "a = " << a << endl; system("pause"); return 0; }



