此文缘起我看了这篇文章:
http://blog.csdn.net/newslxw/archive/2006/08/08/1038972.aspx
还记得当年读书时, 牛牛的C++老师告诉我: "引用, 其实就是别名, 与其所引用的对象完全等价". 可是, 我还是有这点疑问: "等价的含义, 是指的什么? 难道是说在最底层的实现上两者直接就是使用的同一个内存位置吗? " 比如下面的这段代码:
int
main()
{
int a = 100;
int &b = a;
int *c = &a;
b = 99;
a = 88;
b = 77;
*c = 66;

return 0;
}

经过汇编后, 变成了这样:
main:
leal
4
(
%
esp),
%
ecx
andl $
-
16
,
%
esp
pushl
-
4
(
%
ecx)
pushl
%
ebp
movl
%
esp,
%
ebp
pushl
%
ecx
subl $
16
,
%
esp
.LCFI5:
movl $
100
,
-
16
(
%
ebp)
; ebp-16 为变量 a 的地址
leal
-
16
(
%
ebp),
%
eax
movl
%
eax,
-
12
(
%
ebp)
; 将a的地址放在 ebp-12 地址处
leal
-
16
(
%
ebp),
%
eax
movl
%
eax,
-
8
(
%
ebp)
; ebp-8是指针c变量, 此句是给指针c赋值
movl
-
12
(
%
ebp),
%
eax
; 给引用b赋值前, 先把存在 ebp-12 的 a 的地址取到eax中
movl $
99
, (
%
eax)
; b = 99
movl $
88
,
-
16
(
%
ebp)
movl
-
12
(
%
ebp),
%
eax
; 此处给引用b的赋值, 与前面一样, 也是先取a的地址
movl $
77
, (
%
eax)
movl
-
8
(
%
ebp),
%
eax ; 给*c赋值前, 先取c地址
movl $
66
, (
%
eax) ; 此处执行 *c = 66
movl $
0
,
%
eax
addl $
16
,
%
esp
popl
%
ecx
popl
%
ebp
leal
-
4
(
%
ecx),
%
esp
ret
由此可见, 引用, 在具体实现时, 会申请一个临时空间(栈内空间), 目的是存放被引用对象的地址. 当使用引用变量时, 其实就是对被引用对象作了一次间接寻址. 而从汇编代码中来看, *c 确实与 b 在使用时的汇编代码一样, 都是先取地址, 再将值存入此地址. 所不同的, 只是在c++层面, 作为如 b 这样的引用类型变量, 只可在初始化时赋值, 且只能赋值一次.
http://blog.csdn.net/newslxw/archive/2006/08/08/1038972.aspx
还记得当年读书时, 牛牛的C++老师告诉我: "引用, 其实就是别名, 与其所引用的对象完全等价". 可是, 我还是有这点疑问: "等价的含义, 是指的什么? 难道是说在最底层的实现上两者直接就是使用的同一个内存位置吗? " 比如下面的这段代码:













经过汇编后, 变成了这样:



























由此可见, 引用, 在具体实现时, 会申请一个临时空间(栈内空间), 目的是存放被引用对象的地址. 当使用引用变量时, 其实就是对被引用对象作了一次间接寻址. 而从汇编代码中来看, *c 确实与 b 在使用时的汇编代码一样, 都是先取地址, 再将值存入此地址. 所不同的, 只是在c++层面, 作为如 b 这样的引用类型变量, 只可在初始化时赋值, 且只能赋值一次.