引用数据类型的 & 符号是否返回引用本身的地址或它所引用的值
Does ampersand of reference data type return the address of the reference itself or the value it is referencing?
我希望我能自己测试它,但不幸的是,在我目前的情况下不可能这样做。有人愿意对此有所了解吗?
它返回它所引用的值的地址。
与指针不同,引用实际上不是对象。它本身没有地址,更容易将其视为编译器随后将解释的对象的别名。
无论如何,您可以随时使用IdeOne在线测试代码。
#include <stdio.h>
void func(int &z)
{
printf("&z: %p", &z);
}
int main(int argc, char **argv)
{
int x = 0;
int &y = x;
printf("&x: %pn", &x);
printf("&y: %pn", &y);
func(x);
return 0;
}
在线执行,有结果。
值的地址。(没有参考地址这样的东西)
您可以随时在代码键盘上检查:http://codepad.org/I1aBfWAQ
进一步的实验:有在线编译器可以检查中间结果!
以 Mahmoud 在 Try out LLVM and Clang 页面上的示例为例,我们得到以下 LLVM IR(截断)。
define i32 @main() nounwind uwtable {
%x = alloca i32, align 4
store i32 0, i32* %x, align 4, !tbaa !0
%1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([8 x i8]* @.str, i64 0, i64 0), i32* %x)
%2 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([8 x i8]* @.str1, i64 0, i64 0), i32* %x)
%3 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([7 x i8]* @.str2, i64 0, i64 0), i32* %x) nounwind
ret i32 0
}
。语法不是太重要,重要的是注意已经声明了一个变量:%x
(其名称与 C 函数的名称可疑地相似)。
这意味着编译器已省略y
和z
变量,不会为它们分配任何存储。
我就不帮你列出;)
相关文章:
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 如何从绝对地址的 C 样式指针创建对C++对象的引用
- 右值引用转换后的地址更改
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 获取通用/前向引用地址有什么意义?
- 按引用传递和按地址传递之间的差异
- C++ / Qt:如何检测主机名或IP地址何时引用当前系统?
- 常量引用函数参数的地址何时唯一?
- 通过引用传递是否涉及复制地址
- 在函数中传递变量的指针和引用/地址
- 取消引用地址时是否可以重定向到其他地址?
- 指针和引用地址
- C++ *和运算符的取消引用地址?
- 通过引用/地址传递SDL窗口
- 通过引用地址传递问题
- 引用地址如何在C++变为 NULL
- CPP新手 - 如何将值从字符串转换为引用(地址)变量
- 使用引用地址的C++运算符重载
- 在 Xcode 6.3 下,NULL C++引用地址的计算结果为非零
- 指向引用/地址参数的指针