In article <11**********************@g43g2000cwa.googlegroups .com>,
<ja**********@yahoo.com> wrote:
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.
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?