假设我有一个接受常量引用参数的函数,
int func(const int &i)
{
/* */
}
int main()
{
int j = 1;
func(j); // pass non const argument to const reference
j=2; // reassign j
}
这段代码完全有效。根据C++ primer的解释,传递给这个函数的参数如下:
int j=1;
const int &i = j;
如果i是j的同义词(alias),并且i被定义为const,那么我的问题是:如果将代码写成
const int &i = j
这样是否重新将非const变量声明为const变量?为什么这个表达式在C++中是合法的?