re-seating a c++ reference

Is this legal in C++?

int x=5;
int y=10;
int& z=x; //Initialize a reference
z=y; //Now change it so that it 'aliases' to another object

It compiles fine.

Why do you think it isn't, or shouldn't be, legal?

C++ yes. C no.
You do realize then that z = y is also x = y?

Why are references not reseatable in C++

Pieter

wonderfuliot:
Is this legal in C++?

int x=5;

int y=10;
int& z=x; //Initialize a reference
z=y; //Now change it so that it 'aliases' to another object


It compiles fine.

It is legal but it doesn't do what the comment says it does. It's roughly equivalent to:

int x=5;
int y=10;
int *z = &x; // Initialize a pointer
*z=y; // Set 'x' to 10 (value of 'y')

If you want to re-direct a reference, use a pointer:

int x=5;
int y=10;
int *z = &x; // Initialize a pointer
*z=y; // Set 'x' to 10 (value of 'y')
z = &y;  // Re-direct the pointer
*z = 15;  // Set 'y' to 15