Why is it illegal/immoral to reseat a reference?

c++, reference

Solution

Because there is no syntax to do it:

int x = 0;
int y = 1;
int & r = x;

Now if I say:

r = y;

I assign the value of y to x. If I wanted to reseat I would need some special syntax:

r @= y;    // maybe?

As the main reason for using references is as parameters and return types of functions, where this is not an issue, it didn't seem to C++'s designers that this was a path worth going down.

Problem

Possible Duplicate: Why are references not reseatable in C++ I am trying to more or less swap two reference variables (as practice, I could have swapped the actual variables). I tried doing this by making a temporary variable and making one of the references equal the other, but this got shot down by the compiler. Here is an example: ``` void Foo() { //code int& ref1 = a; int& ref2 = b; int temp; temp = ref1; ref1 = ref2; ref2 = temp; //or, better yet std::swap(ref1, ref2); } ``` I got an error, and looked on the faq lite. It details that they cannot be reseated, but does not explain why. Why? Here is a link to the Faq Lite for reference (<---, get it?).

Original source

Related problems