holotko wrote:

> In C++ when passing values to a function we can pass values by
> "reference" like so:
> 
>  int foo(int& int&); // prototype for foo()
> 
> main()
> {
> 
>  int x = 10;
>  int y = 20;
>  int result;
>   
>  result = foo(x, y); // The values of x and y passed to  
> 
> }
> 
>  And foo() defined as:
> 
>   int foo(int& a, int& b)
>   {   
>     int value;
>      ...
>     return value;
>    } 
> 
> One possible advantage to passing values this way is that in addition
> to returning a value to main() side effects are produced,i.e. if foo()
> modifies a or b the change will be reflected in the values of x or y
> in main, if such effects are desired.
> 
> Pretty much the same effect can be easilly acheived via passing
> arguments as pointers (which is how I normally go about such a thing).
> My main 2 questions are:
> 
> 1) What is the overall advantage of passing arguments this way (by
> reference), as oppsoed to using pointers , and,

None. The above code is equivalent to having `foo' declared as

        int foo(int* a, int* b);

and using `*a' and `*b' in the body. C++ references are merely
`syntactic sugar' for using pointers. Personally I dislike this
feature, as it makes it harder to see what is really going on.

> 2) When passing values this way what is actually being passed to foo()
> ?? Is it the address of x & y (which would imply pretty much the same
> as using a pointer),

Yes.

> or an alias which references the adress of the args passed to
> foo()???

Yes.

Both of these seem to be the same thing to me.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to