问题描述
double&val = 66.6; //非法
const double&val = 66.6; // legal
我刚刚做了一些演示程序,并通过上述概念,但无法确定上述概念的确切需求。 const在第二种情况下究竟做了什么魔术?
有谁可以让我知道我们在实时编程中可以使用这个概念到底在哪里?
double &val = 66.6; //illegal
const double &val = 66.6; //legal
I was just doing some demo programs and came through the above concept but not able to identify what exactly the need of the above concept . what magic exactly const is doing in the second case ?
can anyone please let me know where exactly we can use this concept in real time programming ?
推荐答案
const double& val = 66.6; // legal
const double &val = 66.6; //legal
您只能将常量用作 rvalue
,因此不需要 val
引用一个实际地址(即编译器可以用常量 66.6
隐式替换每次出现的val。)
注意你不能将相同的参数应用于
You may use a constant only as rvalue
hence there is no need for val
to refer to an actual address (that is the compiler may implicitly substitute every occurrence of val with the constant 66.6
).
Note you cannot apply the same argument to the
double& val = 66.6; //非法
double &val = 66.6; //illegal
行。
这篇关于使用常量修饰符引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!