【C++对C语言的拓展】引用--3

1.8 指针引用

#include  <iostream>
using namespace  std;  
struct  Teacher  
{  
char  name[64];  
int  age  ;  
};  
//在被调⽤用函数 获取资源
int  getTeacher(Teacher  **p)  
{  
       Teacher  *tmp  =  NULL;  
if  (p  ==  NULL)  
       {  
return  -­‐1;  
       }  
       tmp  =  (Teacher  *)malloc(sizeof(Teacher));  
if  (tmp  ==  NULL)  
       {  
return  -­‐2;  
       }  
       tmp-­‐>age  =  33;  
//  p是实参的地址    *实参的地址 去间接的修改实参的值
       *p  =  tmp;  
return 0;  
}  
//指针的引⽤用 做函数参数
int  getTeacher2(Teacher*  &myp)  
{  
//给myp赋值 相当于给main函数中的pT1赋值
       myp  =  (Teacher  *)malloc(sizeof(Teacher));  
if  (myp  ==  NULL)  
       {  
return  -­‐1;  
       }  
       myp-­‐>age  =  36;  
return 0;  
}  
void  FreeTeacher(Teacher  *pT1)  
{  
if  (pT1  ==  NULL)  
       {  
return  ;  
       }  
       free(pT1);  
}  
int  main(void)  
{  
       Teacher  *pT1  =  NULL;  
//1  c语⾔言中的⼆二级指针
       getTeacher(&pT1);  
       cout<<"age:"<<pT1-­‐>age<<endl;  
       FreeTeacher(pT1);  
//2  c++中的引⽤用 (指针的引⽤用)
//引⽤用的本质 间接赋值后2个条件 让c++编译器帮我们程序员做了。
       getTeacher2(pT1);  
       cout<<"age:"<<pT1-­‐>age<<endl;  
       FreeTeacher(pT1);  
return 0;  
}  

1.9 const 引用
const  引用有较多使用。它可以防止对象的值被随意修改。因而具有一
些特性。    
  (1)const  对象的引用必须是  const  的,将普通引用绑定到  const  对象是不
合法的。 这个原因比较简单。既然对象是  const  的,表示不能被修改,引用当然
也不  能修改,必须使用  const  引用。实际上,
      const  int  a=1;  
      int  &b=a;
这种写法是不合法  的,编译不过。  
  (2)const  引用可使用相关类型的对象(常量,非同类型的变量或表达式)初
始化。 这个是  const  引用与普通引用最大的区别。
      const  int  &a=2;
是合法的。
      double  x=3.14;  
      const  int  &b=a;
也是合法的。    

#include  <iostream>
using namespace  std;  
int  main(void)  
{  
//普通引⽤用
int    a  =  10;  
int  &b  =  a;  
       cout  <<  "b  =  "  <<  b  <<  endl;  
//常引⽤用
int    x  =  20;  
const int  &y  =  x;      //常引⽤用是限制变量为只读 不能通过y去修改x了
//y  =  21;                      /error
return 0;  
}

1.10 const引用的原理
  const  引用的目的是,禁止通过修改引用值来改变被引用的对象。const  
引用的  初始化特性较为微妙,可通过如下代码说明:

double  val  =  3.14;  
const int  &ref  =  val;  
double  &  ref2  =  val;  
cout<<ref<<"  "<<ref2<<endl;  
val  =  4.14;  
cout<<ref<<"  "<<ref2<<endl;  

上述输出结果为  3  3.14  和  3  4.14。因为  ref  是  const  的,在初始化的过
程中已经给定值,不允许修改。而被引用的对象是  val,是非  const  的,所以  val  
的修改并未  影响  ref  的值,而  ref2  的值发生了相应的改变。    
  那么,为什么非  const  的引用不能使用相关类型初始化呢?实际上,const  
引用  使用相关类型对象初始化时发生了如下过程:  
int  temp  =  val;  
const int  &ref  =  temp;
  如果  ref  不是  const  的,那么改变  ref  值,修改的是  temp,而不是  val。期
望对  ref  的赋值会修改  val  的程序员会发现  val  实际并未修改。  

#include  <iostream>
using namespace  std;  
int  main(void)  
{  
//1>  ⽤用变量 初始化 常引⽤用
int  x1  =  30;  
const int  &y1  =  x1;  //⽤用x1变量去初始化 常引⽤用
//2>  ⽤用字⾯面量 初始化 常量引⽤用
const int  a  =  40;    //c++编译器把a放在符号表中
//int  &m  =  41;  //error  ,  普通引⽤用 引⽤用⼀一个字⾯面量 请问字⾯面量有没有内存地址
const int  &m  =  43;    //c++编译器 会 分配内存空间
//  int  temp  =  43
//  const  int  &m  =  temp;
return 0;  
}
#include  <iostream>
using namespace  std;  
struct  Teacher  
{  
char  name[64];  
int  age  ;  
};  
void  printTeacher(const  Teacher  &myt)  
{  
//常引⽤用 让 实参变量 拥有只读属性
//myt.age  =  33;
       printf("myt.age:%d \n",  myt.age);  
}  
int  main(void)  
{  
       Teacher    t1;    
       t1.age  =  36;  
       printTeacher(t1);  
return 0;                                
}

结论:
1)const int & e 相当于 const int * const e
2)普通引用 相当于 int *const e
3)当使用常量(字面量)对const引用进行初始化时,C++编译器会为常量值
分配空间,并将引用名作为这段空间的别名
4)使用字面量对const引用初始化后,将生成一个只读变量
 

 

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IIIIIII_II

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值