1

Say I have a this code:

class A{
    ...
};

//somewhere else
A a;
A & ref = a;
a = A();

Does ref still reference the a? Regardless of how many assignments happen? That is, the assignment operator would never cause a memory location change, right?

user975989
  • 2,578
  • 1
  • 20
  • 38
  • 1
    References can not be reassigned. – Jonathan Potter Jul 25 '15 at 04:28
  • Read from right to left whe dealing with pointers/references. So, a is an instance of class A and ref is now a reference to class A which has been assigned to a( which is an instance of class A, a bit redundant but its your example) and lastly, you assigned A to class A in a different way. Take in mind what @JonathanPotter said as well, once a reference is initialized it cannot be reassigned.If youre intending on doing so use pointers – TimLayne Jul 25 '15 at 04:48
  • there is no difference between a pointer and a reference in the sense of what they are pointing to. There are certain limitations to using references like the fact that they can't be reassigned but nothing magical is happening because you are using references –  Jul 25 '15 at 04:56

3 Answers3

2

Yes it does so. Actually after the last line ref will point to whatever a points to.

Whatever you do to the reference is also done to the original object.

So all you do is defining an alternative name for the same variable.

There are few things you need to remember

i) references are different from pointers

ii) pointer may be undefined /null but reference should always be associated with a variable.

iii) pointer may be able to point to different variable at different time; reference always associated with the same variable throughout it's life. Check this question

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • But say `a` was at location 0x0004 or something. `a` will stay at 0x0004 right? Because I am storing a pointer to `a` somewhere else as well. – user975989 Jul 25 '15 at 04:25
  • @coderredoc `ref` cannot point to a new object. It will point to the same object! But, the value returned by `A()` will be copied into `a` by the assignment operator. – user007 Jul 25 '15 at 04:38
  • 1
    "refrences are automatically derefrenced." References don't need to be dereferenced. They are references. – AnT stands with Russia Jul 25 '15 at 04:38
  • Point iii) is not true – Ed Heal Jul 25 '15 at 04:43
  • @coderredoc http://ideone.com/7yR6sS I might be still mis-interpreting you.. But I am talking about the first line of your answer ".... after the last line ref will point to a new object same as that of a... ", it seems like you are trying to say that `a` will be a new object, and so the `ref` will now point to the new `a`, which is not true. If you check the link, you will see that `&A`(in the link) doesn't change. – user007 Jul 25 '15 at 04:49
  • 1
    @EdHeal : `ref` doesn't change. Its value changes. http://ideone.com/a1ygCt (All `&x` print the same value) – user007 Jul 25 '15 at 04:54
  • You are confusing implementation of references with its semantics. The compiler that you are using has chosen one way of implementing them – Ed Heal Jul 25 '15 at 04:57
  • 1
    @EdHeal.: Check the answer http://stackoverflow.com/questions/9293674/can-we-reassign-the-reference-in-c – user2736738 Jul 25 '15 at 05:00
  • I stand (well sat) down corrected., In my defence it is 6AM here and the neighbour has had a party all night :-( – Ed Heal Jul 25 '15 at 05:03
  • ,,, that is a little cheeky – Ed Heal Jul 25 '15 at 05:13
  • @EdHeal.: Hey man! you have upvoted..you don't need to..was just joking..I myself got confused at your statement..you don't need to ..Sorry – user2736738 Jul 25 '15 at 05:16
2

Does ref still reference the a?

Yes . Look at the code below. It shows how the assignment does not change the memory location...

A a;
std::cout<<"a:"<< &a<<std::endl;
A & ref = a;
std::cout<<"ref:"<< &ref<<std::endl;

a = A();
std::cout<<"a:"<< &a<<std::endl;

The output looks like so:

a:0x7fffaaa5fcaf
ref:0x7fffaaa5fcaf
a:0x7fffaaa5fcaf
wizurd
  • 3,541
  • 3
  • 33
  • 50
0

Does ref still reference the a?

Yes.


A reference variable is an alias for another variable.
ref is bound to a, to it's original variable memory. Assigning something to a will also affect ref since it refers to the memory of a. Below is a small example:

class A {
public:
    A(int x, int y) : m_x(x), m_y(y) {}
    A() = default;
    int m_x;
    int m_y;
};

int main()
{
    A a;
    A &ref = a;

    a = A(500, 500);

    cout << ref.m_x << " " << ref.m_y << endl;
}

Output is 500 500

Andreas DM
  • 10,685
  • 6
  • 35
  • 62