References

Dec 15, 2010 at 1:00am
I was thinking, if references are implemented as pointers, why is there NO way to change the object it points to? I mean, I get that something as simple as this:

1
2
type& typeref=var;
typeref=42;

would cause issues about whether you're assigning "42" to rhe pointer or what it points to, but I don't ubderstand why they didn't make an extra operator to do it, maybe something like this:
1
2
3
4
5
6
7
8
type& typeref=var;
//assign 42 to var
typeref=42;
//reassign typeref to a new variable
*typeref=var2;
//sort of like a reverse pointer -
// *pointer dereferences pointer, so
// *reference could rereference reference (give the pointer) 

So, why didn't they do that?
Dec 15, 2010 at 1:36am
Because conceptually references and pointers are different (even if they are implemented the same way sometimes -- but even that isn't necessarily guaranteed)

If you want it to be able to be reseated, use a pointer. One of the reasons to use a reference is the fact that it can't be reseated.
Dec 15, 2010 at 2:32am
closed account (3pj6b7Xj)
A reference is more like a const pointer, sort of.......you can obtain a reference to a structure but you CAN'T modify the structure, you can however, obtain a pointer to a structure but you can modify the structure the pointer points to by using -> to change any of its member variables. I've seen some really confusing uses of references and pointers. I think Microsoft's use of a pointer to a pointer is kind of strange, you know the p** double star that looks very ugly.
Dec 15, 2010 at 4:01am
From what I've read on this site, pointers and references produce exactly the same code. 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 - you would get the same thing if you used a constant reference, and you would have the added benefit of being able to reseat it if it's not constant. I simply don't understand... Wasn't the point of C/C++ supppsed to be to give prpgrammers as much freedom as possible?
Dec 15, 2010 at 4:17am
If you want the same functionality of pointer for reference then the introduction of reference in C++ would be redundant isn't it ?

I believe the introduction of reference variable is NOT supposed to replace pointer. The original intention to introduce reference I guess we need to refer back to C++ creator Bjarne Strostrup isn't it ? :)

Dec 15, 2010 at 4:42am
Redundant, yes, but like was already said, references are redundant anyway. They are essentially constant pointers with difderent syntax.

Personally, I like the syntax used by references because "." is easier to type then "->" (I know that's extremely lazy) and it looks nicer.

Also, I'm not quite sure what you're saying in your last sentence...
Dec 15, 2010 at 5:05am
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.
Last edited on Dec 15, 2010 at 5:06am
Topic archived. No new replies allowed.