J
John G Harris
I checked in MS 2005 compiler. It is so, C++ reference concept *is
just a syntactic sugar* for a pointer. I can statement it now
precisely.
You've proved that in one circumstance a reference parameter can be
implemented using a pointer. Nobody has said it can't be.
It doesn't prove that there is no other way.
Example to check: <URL: http://gist.github.com/380515>
1 C++ source:
2
3 void call_by_reference(int& x)
4 {
5 x = 20;
6 }
Note that the y declared in main is not in scope here.
8 void call_by_pointer(int* x)
9 {
10 *x = 20;
11 }
12
13 void by_value(int x)
14 {
15 x = 20;
16 }
17
18 int _tmain(int argc, _TCHAR* argv[])
19 {
20 int y = 0;
21
22 call_by_reference(y);
y is not in the scope of the body of call_by_reference so the caller
must supply its address because the function body cannot find the
variable in any other way.
23 call_by_pointer(&y);
24 by_value(y);
25
26 return 0;
27 }
28
29 Disassembly:
30
31 The most important lines are 50-51 (by-reference) and 55-56 (by-
pointer)
32 as we see, the same -- load effective address and push to stack.
33
34 int _tmain(int argc, _TCHAR* argv[])
35 {
67 }
P.S.: Also, C++ creator Straustrup also notice that at implementation
level, a reference is just a sugar for the pointer. And this pointer
is dereferenced every time when we deal with a reference operation.
What Stroustrup says in the 3rd edition of his book is this :
"A reference is an alternative name for an object."
and
"In some cases, the compiler can optimize away a reference so that there
is no object representing that reference at run-time."
John