PDA

View Full Version : Understanding C++ References (&)

Laharl1234
07-22-2008, 02:24 AM
I've started functions and am a bit confused about referencing. Look at this code please:

#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}

int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}

Now I understand that the "&" within the duplicate parameters allow integers x, y, and z to refer to a, b, c, multiplying them by two, but I don't understand this:

#include <iostream>
using namespace std;

void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}

int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}

What is being referred to here? I don't understand how y and z suddenly become the result of prev and next. Please help!

ralph l mayo
07-22-2008, 04:04 AM
Which part of the second makes it seem different to you than the first?
In fact, they're nearly identical.
I don't know if it will help but we can grow them together a little bit and maybe isolate your confusion.

y *= x;

Is a shortcut for

y = y * x;

So we can rewrite the first function like this without changing its functionality at all:

void duplicate (int& a, int& b, int& c)
{
a = a * 2;
b = b * 2;
c = b * 2;
}

If we substitute some similarly poorly named variables in the second problem we get:

void prevnext (int x, int& a, int& b)
{
a = x-1;
b = x+1;
}

Which doesn't get us anywhere, really, but maybe it makes the similarity a little more apparent. The only difference is the math operation you're using and the fact that the first parameter is passed by-value in the second function.

Possibly confusing general explanation:

Each variable in C++ has two components: a value, and an address.
To go along with this distinction there are three argument conventions:
1. by value (eg, typename varname with no modifier, int x)
These values are copied to their new temporary name when you call the function. You cannot modify the caller's copy of a variable passed by value by any means. The address of the passed variable in the caller's scope and the local variable in the function's scope are different. That means if you put new stuff in the function copy it will never extend outside of the function unless you return that value.
2. by pointer to address (eg, typename* varname, int* x)
I take it you haven't learned about this yet, but long story short, a 'pointer' is exactly what it sounds like, directions to find a variable in memory. Since a pointer variable is dealing with the same memory, if you change it, you change the value that was passed because there is no copy.
3. by reference (eg, typename& varname, int& x)
A reference can be thought of as *exactly the same thing* as the variable it references. It has the same value and the same address. References behave like pointers in that anything you do to them happens to the same memory location as the caller's version of the value, so changes are replicated across the scope of the function. The only difference between references and pointers is semantics-- pointers involve special syntax to toggle between the value they point at and their intrinsic value (the memory address), which generally makes them a pain in the ***.

To gloss over some nuances, you can say that a by-value parameter passes "value" (duh), pointer passes "address", and reference passes "address AND value"

In the likely event that didn't help at all, please post again elaborating why you find the second function confusing but not the first.

Laharl1234
07-22-2008, 01:55 PM
Excellent. I understand now. So it was only the variable names that made it confusing for me.