reference variables

R

Rahul

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?
 
S

Salt_Peter

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?
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top