A reference variable's address and value C++

I know that a reference variables's address cannot be changed once set,

References have nothing to do with addresses. Don't think of references as special pointers.

Unless you are talking about how a compiler might implement references. But this is a completely different level of abstraction. On a C++ programmer level, you are not looking at the language from the same point of view as a compiler writer; there is no such thing as a "reference variable's address". Objects have addresses, and a reference is just a different way to name an object.

Or, as I like to explain it to beginners, a reference is the object.

but when I run the command how is my reference's value change but the address is the same?

In the following line:

int &ref = a;   //&ref is 0x28fee

You are declaring ref to be a reference to a. Using the explanation from above, ref is a. So when you then go on and write &ref afterwards (using the & character to invoke the address-of operator, rather than declaring a reference), what happens is equivalent to what happens when writing &a. It is therefore not surprising that the result is identical. It's one and the same object, so the address must be the same.

ref = b;        //value of ref = 5, address is still 0x28fee

This is the same as writing a = b;. It assigns the value of b to a, because ref is just another way to say a. No addresses are changed when values are assigned to ints.

Lastly, is it correct to assume that the "&" sign is same the one we use in pointers?

No, not at all. Reiterating my point from above: & has different meanings in C++. You use it to declare references or to take addresses of objects. Actually, there are even more meanings, considering that & is also the bit-wise AND operator, as in a & b. Pairs of &, i.e. &&, extend the list of possible meanings to include the logical AND operator and C++11 rvalue references.


From a teaching point of view, it is of course unfortunate that & is overloaded for reference declarations and the address-of operator. As far as I know, & was chosen for references because back then, it was difficult to add new keywords (like ref) to C++ as backward compatibility with C was a huge concern at that time, and adding ref as a keyword would have meant that C code like int ref = 0; would no longer compile.

C++ had to introduce at least some new keywords (e.g. throw), but in certain cases it was technically possible and/or politically necessary to do without them.


when I run the command how is my reference's value change but the address is the same?

reference is an alias, when you assign value to ref you actually assign it to variable a. Why would you expect address to change?

or maybe shouldn't a's value change into 5

this is correct, when you change value of reference ref in your example, you can as well substitiute ref to a, so you have a = b.

Lastly, is it correct to assume that the "&" sign is same the one we use in pointers?

I am not sure what you ask here, & is used to designate that you want a reference, it is not used here to take address of variable a.

Tags:

C++

Reference