深入理解C++ 的 引用

本文详细介绍了C++中的引用,包括引用的初始化、作为函数参数、作为函数返回值的注意事项,以及引用的本质。同时,讨论了常量引用的使用,包括其不可修改的特性及其在函数参数中的应用。通过示例代码,展示了引用如何工作以及常量引用如何防止值被意外修改。
摘要由CSDN通过智能技术生成

目录

1.引用

2.引用做函数参数

3. //引用作函数的返回值

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

//2.函数调用作为函数的左值

4.引用的本质

//reference

//pointer

5.常量引用-常量的引用

//1.

//const

//2.函数形参 const 与 非 const


1.引用

       int a=10;

       int b=20;

       //int &c; //需要初始化

       int &c=a;//不能更改

       c=b;     //赋值操作 非更改引用

       //&c=b;  //不能更改

       cout<<a<<endl;

       cout<<b<<endl;

       cout<<c<<endl;

//    int &&d=b;  //不存在引用的引用

       int *p=&a;

       int *&e=p; //指针的引用

//    int &*f=&b; //不存在引用的指针

2.引用做函数参数

void swap(int& a,int& b);

int _tmain(int argc, _TCHAR* argv[])

{

       int a=10;

       int b=20;

       swap(a,b);

       cout<<"a="<<a<<"b= "<<b<<endl;

//引用作函数参数

      

       return 0;

}

void swap(int& a,int& b)

{

       int temp;

       temp=b;

       b=a;

       a=temp;

}

3. //引用作函数的返回值

//

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

int & fun()

{

       int a=10;//局部变量存放在栈区

       return a;

}

int _tmain(int argc, _TCHAR* argv[])

{

       int &b=fun();

       cout<<b<<endl;//编译器做了一次保留

       cout<<b<<endl;//乱码

       return 0;

}

//2.函数调用作为函数的左值

int & fun()

{

       static int a=4;//静态变量 在全局区 可以返回

       return a;

}

int _tmain(int argc, _TCHAR* argv[])

{

       int &ref=fun();

       cout<<ref<<endl;  //4

       cout<<ref<<endl;  //4

       fun()=1000;// 函数的返回值为引用 函数调用可以作为左值;

       cout<<ref<<endl;  //1000

       cout<<ref<<endl;  //1000

       return 0;

}

4.引用的本质

//ref:指针常量(指针是常量):指针的指向不能改变

//常量是指针(常量指针)

//int &ref=a;

//<==>  int* const ref=&a;

int main()

{

//reference

       int m=1;int n=2;

       int a=10;

       int &ref=a;

       ref=100;

       cout<<"a="<<a<<endl;

       cout<<"ref="<<ref<<endl;

       ref=2;//ok,只是赋值

       //&ref=n; //error 不能改变引用;

//pointer

       int b=10;

       int *const p=&b;

       *p=100;

       cout<<"b="<<b<<endl;

       cout<<"*p="<<*p<<endl;

       *p=n;//ok, 只是赋值

       //p=&n; ///error 不能改变指向

       return 0;

//指针常量

Int *const p;

//常量指针

//int const *p 或者 const int *p

//指向常量的常指针

const int * const p;

5.常量引用-常量的引用

void test01(int &a)

{

       a++;

       cout<<"in test: a="<<a<<endl;

}

void test02(const int &b)

{

       //b++;改不了

       cout<<"in test: b="<<b<<endl;

}

int main()

{

//1.

//const

       //int &ref=10;  error

       const int &ref=10;//编译器的操作:int temp=10; &ref=10;

       //ref=20;//只读不可更改

//2.函数形参 const 与 非 const

       int a=10;

       cout<<"in main: a="<<a<<endl;

       test01(a);

       cout<<"in main: a="<<a<<endl;     

       int b=10;

       cout<<"in main: b="<<b<<endl;

       test02(b);

       cout<<"in main: b="<<b<<endl;

借鉴:黑马程序员匠心之作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值