再谈引用

1.   任何情况下,不能使用指向空值的引用,引用必须指向某些对象


 int *p=0; //set the pointer null

 int &q =*p; //quote point to null  ,harmful


 

2.  在申明引用的时候必须初始化


string &str;   //error,must initialize

string str="it is ok";

string &strRef=str //Ok!



string *str // is ok but dangerous


 

3. 不存在执行空值的引用,所以在使用引用时,不需要测试它的合法性

 


void printDouble( const std::double &refd)

{

     std::cout<<refd<<std::endl;

}



void printDouble(const std::double *pntd)

{

    if(pntd);

    std::cout<<*pntd<<std::endl;

}


 

4.  引用初始化时与变量绑定,以后不会改变


string s1="hai";

string s2="zhang";

stirng &r1=s1;

string *p1=&s1;

r1=s2;// r1仍指s1,但它的值为"zhang",s1变了

p1=&s2; //p1指向s2,s1没有变


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值