# reference variables

Discussion in 'C++' started by Rahul, Dec 18, 2007.

1. ### RahulGuest

Hi Everyone,

I just tried the following code, in response to some post on
reference member variable and i'm not able to understand what exactly
is happening over here,

int main()
{
int a=5,b=6;
int &ar = a;
int &br = b;
printf("ar is %d and br is %d\n",ar,br); //prints 5 and
6
ar = br;
printf("ar is %d and br is %d\n",ar,br); //prints 6 and
6
printf("a is %d and b is %d\n",a,b); // prints 6 and
6, not sure how a's value got modifed
ar = 10;
printf("a is %d and b is %d\n",a,b); // prints 10 and
6
return(0);
}

Could anyone throw in some light, thanks in advance!!!

does ar=br copy the value of b into a?
Rahul, Dec 18, 2007

2. ### LRGuest

LR, Dec 18, 2007

3. ### Salt_PeterGuest

On Dec 17, 9:55 pm, Rahul <> wrote:
> Hi Everyone,
>
> I just tried the following code, in response to some post on
> reference member variable and i'm not able to understand what exactly
> is happening over here,
>
> int main()
> {
> int a=5,b=6;
> int &ar = a;
> int &br = b;
> printf("ar is %d and br is %d\n",ar,br); //prints 5 and
> 6
> ar = br;
> printf("ar is %d and br is %d\n",ar,br); //prints 6 and
> 6
> printf("a is %d and b is %d\n",a,b); // prints 6 and
> 6, not sure how a's value got modifed
> ar = 10;
> printf("a is %d and b is %d\n",a,b); // prints 10 and
> 6
> return(0);
>
> }
>
> Could anyone throw in some light, thanks in advance!!!
>
> does ar=br copy the value of b into a?

Consider the integer variable 'a' itself.
That identifier refers to what would otherwise have been an anonymous
variable.
You can modify a's value only because 'a' is a reference to that
allocation.

'ar' is yet another reference to the same variable.
So whether you use 'a' or 'ar' it doesn't change a thing, both refer
to one and the same.

a = b;
ar = b;
a = br;
ar = br; // pick one, doesn't matter which

You have a nickname, lets suppose its Ra.
Ra is a reference to you, Rahul.
Whether i give Ra or Rahul a beer, whats the difference?
Salt_Peter, Dec 18, 2007
4. ### James KanzeGuest

On Dec 18, 3:55 am, Rahul <> wrote:

> I just tried the following code, in response to some post on
> reference member variable and i'm not able to understand what exactly
> is happening over here,

I'm not sure that you can speak of "reference variables". A
variable normally is an object with a name; a reference is not
an object. (I think the standard may speak of reference
variables, but I'm not sure that it's a good idea
pedagogically.)

Anyway...

> int main()
> {
> int a=5,b=6;
> int &ar = a;

So ar is another name for a.

> int &br = b;

And br is another name for b.

> printf("ar is %d and br is %d\n",ar,br); //prints 5 and 6
> ar = br;

Which is exactly the same (here) as if you'd written:

a = b ;

since ar is just another name for a, and br another name for b.

> printf("ar is %d and br is %d\n",ar,br); // prints 6 and 6
> printf("a is %d and b is %d\n",a,b); // prints 6 and 6,
> //not sure how a's value got modifed

You modified it in the assignment above.

> ar = 10;
> printf("a is %d and b is %d\n",a,b); // prints 10 and 6
> return(0);
> }

> Could anyone throw in some light, thanks in advance!!!

No real problem: references are not objects---once initialized,
they "represent" a previously existing object, and all actions
on the reference actually act on the refered to object.

> does ar=br copy the value of b into a?

It assigns b to a. (In the case of int, this can be thought of
as copying the value, but not in general.)

And while I'm at it: you really should avoid printf and company.
They're definitely not for beginners---you'd be much better
using the simpler and more elegant ostream.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze, Dec 18, 2007