-2
int returnByValue()
{
return 5;
}
int main()
{
const int &cref = returnByValue(); // case I
int &ref = returnByValue(); // case II -- compilation error
return 0;
}
引用變量是一個別名,也就是一個值的別名,那麼case I沒有錯誤,因爲returnByValue()返回的值是一個右值II有編譯錯誤?從一個右值初始化非const引用
你的假設是錯誤的。引用不是變量的別名。它是*值*的別名。 –
是的,一個值的別名(「已有變量的另一個名稱」表達了相同的信息)。 –
對不起,這裏有什麼問題? –