在下面,会在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