Understanding reference variables

In an article I read, the author made the claim that references were like constant pointers, but many of the commenters argued against this claim.
But his claim makes sense in my mind because reference variables can only be associated with one and only one other variable right?
1
2
3
4
int var(1);
// Why aren't these two similar?
int& myref = var;
int* const myptr = &var;


Edit: Moved "const" qualifier to correct position.
Last edited on
It would actually be int* const myptr = &var;

What were the commenters' arguments?

The only tangible difference afaik is that references are (assumed to be) always valid, while pointers are not. This means that references are a bit safer to use. Another argument might be that a reference is essentially using a different alias for the same object, while a pointer is slightly less direct in that you must dereference first. I doubt that there's any difference in compiler output between the two, however.

EDIT: lots of rephrasing
Last edited on
Thanks for correcting me Branflakes.

Link to the article:
http://www.codeproject.com/Articles/13363/An-Insight-to-References-in-C

Some things the commenters say:
Rob Hemstede wrote:

You state that : "References are nothing but constant pointers" I would agree to say that a reference is a pointer, but not a const pointer. Consider the following code :

int a( 10 );
int b( 20 );

int& ra = a;
ra = b; //<<- OK

int* const pa = &a;
pa = &b; //<<- Error

Yes, they must be initialized but not because they are handled as a const pointer. Maybe its just to enforce some programming sanity.

....

Sorry if I was a bit unclear. My only point was that the compiler does not handle references like const pointers. The code shows that, unlike const pointers, references can be changed after initialization.

ra = b; //<<- Compile OK
pa = &b; //<<- Compile error
Iftahh wrote:

....
but I agree reference != const pointer for other more subtle differences.
So he's just pointing out that they are semantically different, but I disagree with his reasoning about why a reference is "not a const pointer."

His example shows how you cannot use one like it is the other, but it doesn't prove that they each can't be used to achieve the same goal.

For example, the equivalent const pointer approach would be this:
1
2
int* const pa = &a;
(*pa) = b;


It's obviously not semantically the same, but it achieves the same exact outcome as the reference example.

The code shows that, unlike const pointers, references can be changed after initialization.

This is a confusing statement. The reference is still constant in the first example - you just can't see it and therefore can't change it. The first example DOES NOT reseat the reference, it just stores the value of b (20) into a. ra is still a reference to a, and always will be. It is essentially a const pointer, just a semantically hidden one.
Last edited on
Ah, okay. Thanks for the clarification!
Topic archived. No new replies allowed.