【发布时间】:2012-02-25 23:36:05
【问题描述】:
假设我有一个接受 const 引用参数传递的函数,
int func(const int &i)
{
/* */
}
int main()
{
int j = 1;
func(j); // pass non const argument to const reference
j=2; // reassign j
}
这段代码运行良好。根据 C++ 入门,传递给该函数的参数如下所示,
int j=1;
const int &i = j;
其中i是j的同义词(别名),
我的问题是:如果 i 是 j 的同义词,并且 i 被定义为 const,代码是:
const int &i = j
redelcare 一个非 const 变量到 const 变量?为什么这个表达式在 c++ 中是合法的?
【问题讨论】:
标签: c++ constants pass-by-reference