C++中的引用
void func(int &a)
{
a = 10;
}
int main()
{
int a = 8;
func(a);
return 0;
}
将以上代码转换成汇编代码如下(仅列出主要部分):
_Z4funcRi:
.LFB2:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
movq %rdi, -8(%rbp)
movq -8(%rbp), %rax
movl $10, (%rax)
leave
ret
main:
.LFB3:
pushq %rbp
.LCFI2:
movq %rsp, %rbp
.LCFI3:
subq $16, %rsp
.LCFI4:
movl $8, -4(%rbp)
leaq -4(%rbp), %rdi
call _Z4funcRi
movl $0, %eax
leave
ret
结论:从中可以看出当引用作为函数参数被调用时,实际传递的是地址


浙公网安备 33010602011771号