C++学习 --- 引用

引用

一、引用的基本使用:

#include <iostream>
using namespace std;
​
int main() {
    
    //引用的基本语法
    int a = 10;
    int &b = a;
​
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    
    //修改引用
    b = 20;
​
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
​
    system("pause");
    return 0;
}

 

二、引用注意事项:

 

 

#include <iostream>
using namespace std;
​
int main() {
    
    //1.引用必须初始化
    int a = 10;
    int &b = a;
    //int &b;       //错误
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    
    //2.引用初始化后,不可以改变
    int c = 20;
    b = c;          //赋值操作,而不是更改引用
​
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;
​
    system("pause");
    return 0;
}

 

三、引用做函数参数

#include <iostream>
using namespace std;
​
//交换函数
//1.值传递
void  swap01(int a,int b) {
    int temp = a;
    a = b;
    b = temp;
​
    /*cout << "swap01 a = " << a << endl;
    cout << "swap01 b = " << b << endl;*/return;
}
​
//2.地址传递
void swap02(int * a, int * b) {
    int temp = *a;
    *a = *b;
    *b = temp;
​
    /*cout << "swap02 *a = " << *a << endl;
    cout << "swap02 *b = " << *b << endl;*/return;
}
​
//3.引用传递,别名a,原名a。属于形参和实参的范围
void swap03(int & a, int & b) {
    int temp = a;
    a = b;
    b = temp;
​
    /*cout << "swap03 a = " << a << endl;
    cout << "swap03 b = " << b << endl;*/
    return;
};
​
int main() {
    int a = 10, b = 20;
    
    //swap01(a,b);
    //swap02(&a,&b);
    swap03(a,b);            //引用的语法更清楚简单
    
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
​
    system("pause");
    return 0;
}

 

四、引用做函数的返回值

#include <iostream>
using namespace std;
​
//1.不要返回局部变量的引用
int & test01() {
    int a = 10;             //局部变量,栈区
    return a;
}
​
//2.函数的调用可以作为左值
int & test02() {
    static int a = 20;      //静态变量,全局区,程序结束后由系统释放
    return a;
}
​
​
int main() {
    int & ref = test01();
​
    cout << "ref = " << ref << endl;//第一次正确,编译器做了一次保留
    cout << "ref = " << ref << endl;//第二次错误,a的内存已经释放,乱码
​
​
    int & ref2 = test02();
    cout << "ref2 = " << ref2 << endl;
    cout << "ref2 = " << ref2 << endl;
​
    test02() = 1000;
    //test02()返回的是a,而ref2是a的别名,
    //故含函数的调用可以作为左值
    
    cout << "ref2 = " << ref2 << endl;
    cout << "ref2 = " << ref2 << endl;
​
    system("pause");
    return 0;
}

 

四、引用的本质

五、常量引用

#include <iostream>
using namespace std;
​
//打印数据函数
void showValue(const int & val) {
    //有const修饰的引用变成只读
    //防止误操作
    //val = 1000; 
    cout << "val = " << val << endl;
}
​
int main() {
    
    int a = 10;
    int & ref = a;
    //错误
    //引用必须引一块合法的内存空间
    //int & ref = 10;   
    
    //合法
    //加上const ,编译器将代码修改:
    //int temp = 10;const int &ref1 = temp; 
    const int &ref1 = 10;
​
    //加入const之后变成只读,不可修改。 
    //ref = 20;
//常量引用
    //使用场景:用来修饰形参,防止误操作
    showValue(a);
    cout << " a = " << a << endl;
​
    system("pause");
    return 0;
}

 

 

posted @ 2021-08-04 19:02  yiwenzhang  阅读(38)  评论(0编辑  收藏  举报