&
) in front of it:
int &ref;
ref
of type integer. Of course this doesn't do anything because you can't assign references to other variables at any other time than declaration. So to make ref
refer to something we have to do it in the declaration:
int &ref = x;
x
and that it is also an integer (int). But after doing this, anything we do to ref
will effect x
. Try this source code (cut and paste).
/* -example-source-1------------------------------------------------- */
#include <iostream.h>
void main()
{
int x = 10; // create integer variable called x
int &ref = x; // make a reference variable that refers to x
cout << "x is " << x << " and ref is " << ref << endl;
cout << "Now we change ref to equal 25 ... " << endl;
ref = 25;
cout << "And now x is " << x << " and ref is " << ref << endl;
}
/* ------------------------------------------------------------------ */
What I have explained so far with reference variables is actually fairly simple then. Just remember these rules:int
).
/* -example-source-2------------------------------------------------- */
#include <iostream.h>
void times2(int &x); // function prototype
void main()
{
int var; // declare var as integer variable
var = 10; // put value of 10 in var
cout << "var is " << var << endl;
times2(var); // call 'times2()' with var as parameter
cout << "var is now " << var << endl;
}
void times2(int &x)
{
x = x * 2;
}
/* ------------------------------------------------------------------ */
In the above example, whatever we did to x
in the function 'times2()' would effect the actual variable we passed to the function. In this case we multiplied x
by 2. We passed var
to times2()
. So x
becomes a reference to var
when we called it. Now we make x
equal itself times 2. And since we passed var
to the function it multiplies THAT by two.times2()
return a value. However in returning you can only get that one value from the function. With references you could get multiple values, like in the following.
/* -example-source-3------------------------------------------------- */
#include <iostream.h>
void times2(int &v1, int &v2); // function prototype
void main()
{
int x,y;
x = 10;
y = 15;
cout << "x is " << x << " and y is " << y << endl;
times2(x,y);
cout << "x is now " << x << " and y is now " << y << endl;
}
void times2(int &v1, int &v2)
{
v1 = v1 * 2;
v2 = v2 * 2;
}
/* ------------------------------------------------------------------ */
v1
and v2
in this example) will not effect what you pass to the function (x
and y
for this case). Why? Because without passing by reference you're simply telling v1
and v2
to contain values. Anyway, try out the source, you'll see. (I added an extra line too, if you noticed):
/* -example-source-4-------------------------------------(Mara-Hong)- */
#include <iostream.h>
void times2(int v1, int v2); // function prototype
void main()
{
int x,y;
x = 10;
y = 15;
cout << "x is " << x << " and y is " << y << endl;
times2(x,y);
cout << "x is now " << x << " and y is now " << y << endl;
}
void times2(int v1, int v2)
{
v1 = v1 * 2;
v2 = v2 * 2;
cout << "v1 is " << v1 << " and v2 is " << v2 << endl;
}
/* ------------------------------------------------------------------ */
Page Content & Design ©1998 By Neil C. Obremski |
![]() home |
![]() prfce |
![]() loop |
![]() refs |
![]() ptrs |
![]() struct |
![]() class |
![]() array |
![]() rsrc |
![]() site |
![]() indx |
Wed May 12 21:45:26 1999 |