V
Victor Bazarov
Neo said:OK.. heres what i understood about references.
void swap(int &a, int &b)
{
int t=a,a=b,b=t;
This declares three objects, 't', 'a', and 'b', all local, all with
automatic storage duration. It does not swap 'a' and 'b' arguments.
It attempts to _hide_ them. Unsuccessfully, of course. Curiously,
if it worked, 'a' would only be hidden after 't' is assigned its value
from the "real" 'a'. And 'b' would only be hidden after the local 'a'
gets its value from the "real" 'b'...
You should probably think of using semicolons instead of commas there:
int t=a; a=b; b=t;
otherwise, it's not well-formed.
}
int main()
{
int c=5,d=10;
swap(c,d);
/* here i thought that when compiling, c++ treats a nd b as c and d.
Yes, and...
So i assumed i cannot use the function swap more than one time as a
and b cannot be substituted by another e and f.
Every time the function is called, its formal arguments are initialised
from the actual arguments. That's the only way to "assign" a reference.
if we indeed can then the compiler is internally looking into the
matter and substituting the pointers as required(we can see this in
the disassembly). this makes the reference arguement as a macro (which
saddens me)
What am i missing?
Probably nothing. There are two kinds of references: those that have the
same scope as the referred objects, and those that don't. The ones that
don't have to be implemented somehow. If the function is inlined, we
might assume that the references in the inner scopes truly become aliases
but not all functions can be inlined. So, an alternative implementation
of references is needed. A pointer is it. Why it would sadden anybody
is beyond my comprehension.
*/
}
V