从反汇编来看,就是传值还是传址。返回值与参数的传递都一样,引用就是传递/返回变量地址,传值就仅仅是传递或返回变量的值。
int &add(int &a, int b)
{return a+=b;
}
void test()
{
int x = 11;
int y = 4;
int result = 0;
result = add(x, y);
}
========================
080483f4 <_Z3addRii>:
80483f4: 55 push %ebp
80483f5: 89 e5 mov %esp,%ebp
80483f7: 8b 45 08 mov 0x8(%ebp),%eax
80483fa: 8b 00 mov (%eax),%eax
80483fc: 89 c2 mov %eax,%edx
80483fe: 03 55 0c add 0xc(%ebp),%edx
8048401: 8b 45 08 mov 0x8(%ebp),%eax
8048404: 89 10 mov %edx,(%eax)
8048406: 8b 45 08 mov 0x8(%ebp),%eax
8048409: 5d pop %ebp
804840a: c3 ret
804840b: 90 nop
0804840c <_Z4testv>:
804840c: 55 push %ebp
804840d: 89 e5 mov %esp,%ebp
804840f: 83 ec 18 sub $0x18,%esp
8048412: c7 45 f4 0b 00 00 00 movl $0xb,0xfffffff4(%ebp)
8048419: c7 45 f8 04 00 00 00 movl $0x4,0xfffffff8(%ebp)
8048420: c7 45 fc 00 00 00 00 movl $0x0,0xfffffffc(%ebp)
8048427: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
804842a: 89 44 24 04 mov %eax,0x4(%esp)
804842e: 8d 45 f4 lea 0xfffffff4(%ebp),%eax
8048431: 89 04 24 mov %eax,(%esp)
8048434: e8 bb ff ff ff call 80483f4 <_Z3addRii>
8048439: 8b 00 mov (%eax),%eax
804843b: 89 45 fc mov %eax,0xfffffffc(%ebp)
804843e: c9 leave
804843f: c3 ret
=============================>
080483f4 <_Z3addii>:
80483f4: 55 push %ebp
80483f5: 89 e5 mov %esp,%ebp
80483f7: 8b 45 0c mov 0xc(%ebp),%eax
80483fa: 01 45 08 add %eax,0x8(%ebp)
80483fd: 8b 45 08 mov 0x8(%ebp),%eax
8048400: 5d pop %ebp
8048401: c3 ret
08048402 <_Z4testv>:
8048402: 55 push %ebp
8048403: 89 e5 mov %esp,%ebp
8048405: 83 ec 18 sub $0x18,%esp
8048408: c7 45 f4 0b 00 00 00 movl $0xb,0xfffffff4(%ebp)
804840f: c7 45 f8 04 00 00 00 movl $0x4,0xfffffff8(%ebp)
8048416: c7 45 fc 00 00 00 00 movl $0x0,0xfffffffc(%ebp)
804841d: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
8048420: 89 44 24 04 mov %eax,0x4(%esp)
8048424: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
8048427: 89 04 24 mov %eax,(%esp)
804842a: e8 c5 ff ff ff call 80483f4 <_Z3addii>
804842f: 89 45 fc mov %eax,0xfffffffc(%ebp)
8048432: c9 leave
8048433: c3 ret