对于一个const T&的初始式不必是一个左值,甚至可以不是类型T,但是非const引用不能绑定非左值(no-lvalue)
非const引用
double &dr =1;      //错误:要求左值
const引用
const double &dr=1;        //OK
对于const引用的解释:
double temp=double(1);    //首先建立一个具有正确值的临时变量
const double &dr=temp;   //而后用这个临时变量作为dr的初始式
例如:

View Code
#include <iostream>
#include <string>
using namespace  std;

void TestFuncOne(const string & str)
{
    cout<<str<<endl;
    return;
}

void TestFuncTwo(string & str)
{
    cout<<str<<endl;
    return;
}

int main()
{
    TestFuncOne("Hello World!");  //正确
    
    TestFuncTwo("Hello World!");  //错误  非const引用不能绑定非左值(no-lvalue)
    
    return 0;
}

本文引用自:http://blog.csdn.net/missvip/article/details/1781154

相关文章:

  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-23
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-05-20
  • 2022-02-11
相关资源
相似解决方案