首页 > 其他 > 详细

const引用

时间:2016-10-08 16:17:11      阅读:99      评论:0      收藏:0      [点我收藏+]
#include <iostream>
using namespace std;
//const引用是指向const对象的引用
int main()
{
    const int val = 1024;
    const int& refval = val;

//    int& ref2 = val;    Error,val是常量
// refval=200         Error,refaval是个常量

    int val2 = 1024;
    const int& ref3 = val2;       //const 引用可以指向非const类型
    
    double val3 = 3.14;
    const int& ref4 = val3;        //可以,但会丢失数据
    /*产生临时变量等价于》》          int temp=val3;                const& int ref4=temp;*/
    cout << "ref4=" << ref4 << endl;
    cout << "val3=" << val3 << endl;

/*    int& ref5 = val3;           Error,不会产生临时变量

}

 

const引用

原文:http://www.cnblogs.com/lancoyun/p/5938995.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!