在下面,會在const引用被用於非const對象之前創建一個臨時對象嗎?對非const對象的const引用
const int y = 2000;
const int &s = y // ok, const reference to const object.
int x = 1000;
const int &r = x; // any temporary copy here?
如果沒有,那麼這是如何工作的?
const int z = 3000;
int &t = z // ok, why can't you do this?
尤其是第一種情況下,r'的'值可以改變,即使它被聲明'const'。 – avakar
通常,編譯器通過一個簡單的指針來實現引用。 –
@quant_dev給出OP的示例,提到'x'的源代碼和提到'r'的源代碼被編譯爲相同的機器代碼。沒有涉及指針(在我見過的所有編譯器上)。不是說它是相關的。 – Cubbi