Doubly-linked lists using references rather than pointers (C++)

Started by
14 comments, last by SiCrane 15 years, 3 months ago
Yeah ... this post right here used to be the home of a real stupid-moment - which is what the next 2 posts from others are about. Anyway I deleted it so no one would get misinformed!!!

[Edited by - popsoftheyear on December 19, 2008 4:43:49 PM]
Advertisement
Quote:Original post by popsoftheyear
if you reassign the reference at some point (which is illegal... in fact I wonder why compilers don't check for this? Must be some good reason...)

What are you talking about? There is absolutely no way to reassign a reference. Do you mean code like this?
Foo& a = b;a = c;

What happens here is that a is introduced as an alias for b, and in the second line c is assigned to b. The reference is not changed in any way by the second line.
Quote:
Passing a reference is much safer because of the reasons you mentioned. But holding onto a reference might be considered more dangerous than holding onto a pointer because if you reassign the reference at some point (which is illegal... in fact I wonder why compilers don't check for this? Must be some good reason...), and you crash at some other point much later on when you go and use the reference, you are going to be quite dissappointed and may not figure out the bug for some time.


You can't reseat, or have "null" references in a well-formed C++ program. The only way to cause that to happen is by having previously invoked undefined behavior elsewhere -- at which point all bets (and guarantees) are off anyway.
Quote:Original post by DevFred
Quote:Original post by popsoftheyear
if you reassign the reference at some point (which is illegal... in fact I wonder why compilers don't check for this? Must be some good reason...)

What are you talking about? There is absolutely no way to reassign a reference. Do you mean code like this?
Foo& a = b;a = c;

What happens here is that a is introduced as an alias for b, and in the second line c is assigned to b. The reference is not changed in any way by the second line.


Right... well don't I feel silly now? Something at some point in time drew that completely illogical conclusion in my mind and it never left. Anyway I deleted the above post in hopes no one reads it and gets misinformed... sorry 'bout that.
Too late I'm afraid :P

But that's my point; I prefer references since they're harder to abuse than pointers. I just don't get how holding on to a reference is any worse than holding on to a pointer. Semantically it's the same thing, surely?
Idiomatically when you use a reference there's the guarantee that the object bound to the reference must outlive the object that contains the reference. Since references can't be reseated, you can't delete the object that the reference refers to and reset the reference to a state where it isn't bound to any object. The entire point of using a reference is to say "this alias for an object is always valid". This sharply constrains the number of situations where holding a reference to an object makes sense.

This topic is closed to new replies.

Advertisement