From what I've read on this site, pointers and references produce exactly the same code. |
This may often be the case, but it is not necessarily always true.
Because of this, I don't quite understand why you wouldn't want to be able to reseat a reference like you can a pointer |
Because conceptually, a reference
is what it's referring.
1 2
|
int a;
int& b = a; // now b "is" a
|
Modifying b is the same as modifying a. It's just like creating another name/alias for an existing variable.
Reseating a reference doesn't make sense when you think of it that way, because if 'b' is 'a', then reseating 'b' would be like reseating 'a' (which doesn't make sense).
What's more, you should never need to do that with references anyway. If you need to reseat, then just use a pointer. That's what they're there for.
Also note that not being able to reseat let's the compiler work in optimizations. Since a pointer can be reseated, the compiler will have to dereference it whenever it's accessed because it's possible it doesn't point to what it pointed to earlier. References on the other hand can be interchanged freely with the referenced variable meaning the compiler has more freedom to get rid of indirections.
Wasn't the point of C/C++ supppsed to be to give prpgrammers as much freedom as possible? |
No. Where did you get that idea? =P
Redundant, yes, but like was already said, references are redundant anyway |
No they're not, they're conceptually different. The problem is you're thinking of them like pointers with different syntax. Stop thinking of them like that and you'll start to get it. They're something else entirely.