Connecting Tech Pros Worldwide Help | Site Map

regarding FAQ 8.5

jammie_linux@yahoo.com
Guest
 
Posts: n/a
#1: Oct 11 '05
Hi,
According to the FAQ [8.5] "How can you reseat a reference to make it
refer to a different object?" , the reference is like a constant
pointer.Then why the following program is not giving any compilation
error/s ?

#include <iostream>
using namespace std;

int main(void)
{
int i=9,j=0;
int &ref=i;
cout<< ref << endl;
ref= j; // I'm changing the reference variable.
cout << ref << endl;
return 0;
}

I used g++ to compile the above program.

Bob Hairgrove
Guest
 
Posts: n/a
#2: Oct 11 '05

re: regarding FAQ 8.5


On 11 Oct 2005 11:47:25 -0700, jammie_linux@yahoo.com wrote:
[color=blue]
>Hi,
>According to the FAQ [8.5] "How can you reseat a reference to make it
>refer to a different object?" ...[/color]

You cannot.
[color=blue]
>... the reference is like a constant
>pointer.Then why the following program is not giving any compilation
>error/s ?
>
> #include <iostream>
>using namespace std;
>
>int main(void)
>{
> int i=9,j=0;
> int &ref=i;[/color]

// OK: ref is an alias for i.
[color=blue]
> cout<< ref << endl;
> ref= j; // I'm changing the reference variable.[/color]

You assign the value of j to i here. You haven't "changed the
reference variable", nor can you.
[color=blue]
> cout << ref << endl;
> return 0;
>}
>
>I used g++ to compile the above program.[/color]

--
Bob Hairgrove
NoSpamPlease@Home.com
Mike Wahler
Guest
 
Posts: n/a
#3: Oct 11 '05

re: regarding FAQ 8.5



<jammie_linux@yahoo.com> wrote in message
news:1129056445.488533.206220@g43g2000cwa.googlegr oups.com...[color=blue]
> Hi,
> According to the FAQ [8.5] "How can you reseat a reference to make it
> refer to a different object?" , the reference is like a constant
> pointer.Then why the following program is not giving any compilation
> error/s ?[/color]

Because it's valid.
[color=blue]
>
> #include <iostream>
> using namespace std;
>
> int main(void)
> {
> int i=9,j=0;
> int &ref=i;
> cout<< ref << endl;
> ref= j; // I'm changing the reference variable.[/color]

This is equivalent to

i = j;

The reference still refers to 'i' (iow 'ref' is still
an alias for 'i').

[color=blue]
> cout << ref << endl;[/color]

This should print 0. Does it?
[color=blue]
> return 0;
> }
>
> I used g++ to compile the above program.[/color]

-Mike


Martin Vorbrodt
Guest
 
Posts: n/a
#4: Oct 11 '05

re: regarding FAQ 8.5


<jammie_linux@yahoo.com> wrote in message
news:1129056445.488533.206220@g43g2000cwa.googlegr oups.com...[color=blue]
> Hi,
> According to the FAQ [8.5] "How can you reseat a reference to make it
> refer to a different object?" , the reference is like a constant
> pointer.Then why the following program is not giving any compilation
> error/s ?
>
> #include <iostream>
> using namespace std;
>
> int main(void)
> {
> int i=9,j=0;
> int &ref=i;
> cout<< ref << endl;
> ref= j; // I'm changing the reference variable.
> cout << ref << endl;
> return 0;
> }
>
> I used g++ to compile the above program.
>[/color]

No, you're not.
In this case ref = j; is equivalent to i = j;
cout << i << endl; after you do ref=j; you'll see for yourself.

Martin


Greg Comeau
Guest
 
Posts: n/a
#5: Oct 11 '05

re: regarding FAQ 8.5


In article <1129056445.488533.206220@g43g2000cwa.googlegroups .com>,
<jammie_linux@yahoo.com> wrote:[color=blue]
>Hi,
>According to the FAQ [8.5] "How can you reseat a reference to make it
>refer to a different object?" , the reference is like a constant
>pointer.Then why the following program is not giving any compilation
>error/s ?
>
> #include <iostream>
>using namespace std;
>
>int main(void)
>{
> int i=9,j=0;
> int &ref=i;
> cout<< ref << endl;
> ref= j; // I'm changing the reference variable.
> cout << ref << endl;
> return 0;
>}
>
>I used g++ to compile the above program.[/color]

That does not "reseat" ref. One way to look at this is to
consider that ref becomes another name for i. Therefore,
since you can say i = j, which doesn't mean i become j
but that j's value is copies into i, the same applies here.
Since ref is another name for i, then ref = j is as if it
were i = j.

If you prefer to stay with the constand pointer analogy you
might have:

int * const ref = &i;

i = j; // as usual
*ref = j; //since *ref is also another 'name' for i, then as if: i = j

ref = &j // error
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Greg Comeau
Guest
 
Posts: n/a
#6: Oct 11 '05

re: regarding FAQ 8.5


In article <dih95j$gev$1@panix2.panix.com>,
Greg Comeau <comeau@comeaucomputing.com> wrote:[color=blue]
>In article <1129056445.488533.206220@g43g2000cwa.googlegroups .com>,
> <jammie_linux@yahoo.com> wrote:[color=green]
>>Hi,
>>According to the FAQ [8.5] "How can you reseat a reference to make it
>>refer to a different object?" , the reference is like a constant
>>pointer.Then why the following program is not giving any compilation
>>error/s ?
>>
>> #include <iostream>
>>using namespace std;
>>
>>int main(void)
>>{
>> int i=9,j=0;
>> int &ref=i;
>> cout<< ref << endl;
>> ref= j; // I'm changing the reference variable.
>> cout << ref << endl;
>> return 0;
>>}
>>
>>I used g++ to compile the above program.[/color]
>
>That does not "reseat" ref. One way to look at this is to
>consider that ref becomes another name for i. Therefore,
>since you can say i = j, which doesn't mean i become j
>but that j's value is copies into i, the same applies here.
>Since ref is another name for i, then ref = j is as if it
>were i = j.
>
>If you prefer to stay with the constand pointer analogy you
>might have:
>
>int * const ref = &i;
>
>i = j; // as usual
>*ref = j; //since *ref is also another 'name' for i, then as if: i = j
>
>ref = &j // error[/color]

Also, remember that most operations on references are
actually operations on the thing it references. IOWs,
in this case, after ref is "bound", operations on ref
are actually operations on i.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Closed Thread