MM> One thing that still holds my interest is how the
MM> compiler handles it automatically. Is the code
MM> generated for passing by reference going to work out
MM> the same as passing a pointer to an object and
MM> subsequently dereferencing that pointer?
For the most part, it's supposed to be "like magic". How a compiler wishes
to implement it is up to them. However, most probably use "hidden" pointer
manipulation for you. This may not be true on certain platforms - there may
be a faster way. (I can't think of one, but it might theoretically be
possible... )
Further, there is one other difference. A reference is actually more like
(but is not exactly) a const pointer. That is, once you've assigned to the
reference, it cannot change to refer to anything else. It can be assigned to
only when it comes into scope - which may be multiple times throughout your
program, such as the parameters to a function. i.e.:
void foo(int& x)
// code omitted
void bar()
{
int i, j;
foo(i);
foo(j);
}
x will refer to i in the first call and j in the second. But inside foo, x
cannot change what it refers to. This would be like:
void foo(int const* x)
// code omitted
void bar()
{
int i, j;
foo(&i);
foo(&j);
}
Again, x can point to i, and to j, but cannot change what it points at inside
of foo.
Hope this helps,
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)
|