声明:大部分知识和代码来自黑马程序员
一.引用和函数参数传递
#include <iostream> using namespace std; int& test_01() { static int a = 10; return a; } int main() { //勿返回局部变量的引用(和地址) 但可以返回静态变量...的引用 int & kk = test_01(); cout << kk << endl; cout << kk << endl; //函数的返回值是引用(本质是返回了一个a的引用 如int& x = a) 则这个函数的调用可以作为左值!!!牛 test_01()= 1000; cout << test_01()<< endl; cout << kk<< endl; }
#include<iostream> using namespace std; //发现是引用 转换成 int * const ref = & a void func(int & ref) { ref = 100; } int main() { int a = 10; int& ref = a; // 本质是指针常量 等效 int * const ref = & a 符合指针常量指向不可改指向的值可以改 ref = 20; // n内部发现ref是引用 则自动识别 * ref = 20 cout << ref << endl; cout << a << endl; func(a); return 0; }