| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Rahul" |
| Date: |
17 Dec 2007 08:55:53 PM |
| Object: |
reference variables |
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?
.
|
|
| User: "Salt_Peter" |
|
| Title: Re: reference variables |
17 Dec 2007 09:43:52 PM |
|
|
On Dec 17, 9:55 pm, Rahul <sam_...@yahoo.co.in> 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?
.
|
|
|
|
| User: "James Kanze" |
|
| Title: Re: reference variables |
18 Dec 2007 02:29:32 AM |
|
|
On Dec 18, 3:55 am, Rahul <sam_...@yahoo.co.in> 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=3D5,b=3D6;
int &ar =3D a;
So ar is another name for a.
int &br =3D b;
And br is another name for b.
printf("ar is %d and br is %d\n",ar,br); //prints 5 and 6
ar =3D br;
Which is exactly the same (here) as if you'd written:
a =3D 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 =3D 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=3Dbr 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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|
| User: "LR" |
|
| Title: Re: reference variables |
17 Dec 2007 09:08:27 PM |
|
|
Rahul wrote:
int a=5,b=6;
int &ar = a;
int &br = b;
does ar=br copy the value of b into a?
http://www.parashift.com/c++-faq-lite/references.html#faq-8.2
LR
.
|
|
|
|

|
Related Articles |
|
|