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?