引用的入门

引用


    引用是C++对C的重要扩充。引用的实质:起别名。
    int &b=a;
    &写到左侧:引用;&写道右侧:取地址。

1.引用基本语法

Type &别名 = 原名    

void test05() {
    int a = 10;
    int& b = a;
    b = 20;
    cout << "a=" << a << ",b=" << b << endl;
}

2.引用必须初始化


    引用初始化后不可修改
    


3.对数组建立引用

void test06() {
    int arr[10];
    for (int i = 0; i < 10; i++) {
        arr[i] = i;
    }
    //给数组起别名
    int(&pArr)[10] = arr;
    for (int i = 0; i < 10; i++) {
        cout << pArr[i] << " ";
    }
    cout << endl;

    //第二种方式 起别名
    typedef int(ARRAYREF)[10];//一个具有10个元素的int类型数组
    ARRAYREF& pArr2 = arr;
    for (int i = 0; i < 10; i++) {
        cout << pArr2[i] << " ";
    }
    cout << endl;
    
}


参数的传递方式


1.值传递


2.地址传递

void mySwap(int* a, int* b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

void test07() {
    int a = 1;
    int b = 2;
    mySwap(&a, &b);
    cout << "a=" << a << ",b=" << b << endl;
}


3.引用传递

void mySwap2(int& a, int& b) {
    int tmp = a;
    a = b;
    b = tmp;
}

void test08() {
    int a = 1;
    int b = 2;
    mySwap2(a, b);
    cout << "a=" << a << ",b=" << b << endl;
}


引用的注意事项


1.引用必须引一块合法的内存空间


2.不要返回局部变量的引用


int& doWork() {
    int a = 10;
    return a;
}

void test09() {
    int& ret = doWork();
    
    //如果函数的返回值是引用,那么这个函数可以作为左值
    cout << "ret=" << ret << endl;

    cout << "ret=" << ret << endl;
}

如果函数的返回值是引用,那么这个函数可以作为左值

int& doWork2() {
    static int a = 10;
    return a;
}

void test10() {
    int& ret = doWork2();

    //如果函数的返回值是引用,那么这个函数可以作为左值
    doWork2() = 1000; 
}


引用的本质


    引用的本质:在C++内部实现是一个指针常量

//发现是引用,转换为int* const ref=&a;
void testFunc(int& ref) {
    ref = 100;//ref是引用,转换为*ref=100
}

void test11() {
    int a = 10;
    int& aRef = a;//自动转换为int* const aRef=&a,这也能说明为什么必须初始化
    aRef = 20;//内部发现aRef是引用,自动帮我们转换为:*aRef=20;
    cout << "a:" << a << endl;//20
    cout << "aRef:" << aRef << endl;//20
    testFunc(a);
    cout << "a:" << a << endl;//100
}


引用的使用场景


1.指针引用


    用一级指针引用 可以代替二级指针
     

//C语言中指针的方式
struct Person {
    int m_Age;
};

//**p:具体的Person对象;*p:对象的指针;p:指针的指针
void allocateMemory(Person** p) {
    *p = (Person*)malloc(sizeof(Person));
    (*p)->m_Age = 100;
}

void test12() {
    Person* p = NULL;
    allocateMemory(&p);
    cout << "p's age:" << p->m_Age << endl;//100
 }
 //C++中利用指针引用开辟空间
void allocateMemoryByRef(Person*& p) {
    p = (Person*)malloc(sizeof(Person));
    p->m_Age = 128;
}

void test13() {
    Person* p = NULL;
    allocateMemoryByRef(p);
    cout << "p'age:" << p->m_Age << endl;
}

2.常量引用

const int a=10;//会分配内存

void test14() {
    //int& ref = 10;//引用了不合法的内存,不行
    const int& ref = 10;//加入const后编译器处理方式为:int tmp=10;const int &ref=tmp;

    //ref=10;
    int* p = (int*)&ref;
    *p = 1000;

    cout << "ref=" << ref << endl;//1000
}


3.常量引用使用场景:用来修饰形参


    修饰形参为只读

void showValue(const int& val) {
    //val += 1000;//只想显示不修改就用const修饰
    cout << "val=" << val << endl;
}

void test15() {
    int a = 10;
    showValue(a);
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值