TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: MICHAEL MCGAVIN
from: DARIN MCBRIDE
date: 1998-03-28 16:55:00
subject: passing by reference

 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)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.