0

In C++, once a reference variable has been defined to refer to a particular variable, can it refer to any other variable?

My code I was using for testing is below:

int r1 =1001;
int r2  =10023;
int &r3 = r1;
r3 = r2;
r3 = 999;
r3 = 555;
int r4 = 11;
r3 = r4;
r3 = 10177;
cout<<r3<<endl;
cout<<r1<<endl;
cout<<r2<<endl;
cout<<r4<<endl;

The output:

10177
10177
10023
11
3
  • 1
    no. you cannot even leave out the initialization for a reference. Commented Apr 26, 2013 at 13:03
  • Which part are you confused about? Commented Apr 26, 2013 at 13:04
  • r3 = r2; is assigning value of r2 to r3 not the reference. So they still point to different memory locations but with same values after this statement. Commented Apr 26, 2013 at 13:46

3 Answers 3

6

Once a reference variable has been defined to refer to a particular variable it can refer to any other variable in C plus plus?

No. References are bound at initialization and cannot be re-bound. After initialization, a reference is just an alias for the object it is bound to - and references must always be initialized.

In other words, whatever you do on the reference is done on the object being referenced. Here, for instance:

int &r3 = r1;

You are binding r3 to r1, so r3 will be an alias for r1 (like an alternative name). This means that the subsequent assignment:

r3 = r2;

Does not re-bind r3 to refer to r2: instead, it ends up assigning r2 to r1. Knowing this, you should be able to figure out the behavior of the rest of your program.

Sign up to request clarification or add additional context in comments.

8 Comments

you could mention about declaration without initialization being illegal for references.
+1. Andy why dont you update your profile? more than thinking about the possible solution to questions and learn when i come here to SO, i'm left wondering who andy prowl is :-).
its great to see your answer along with other great answers on SO but this will always haunt me:-) hope you update your profile soon :-)
@Koushik: Sooner or later I will, but don't be disappointed after seeing I'm really noone ;)
i need to thank you for giving me chance to learn.:-). looking forward to seeing more wonderful answers from you. all the best and please complete the favour soon:-)
|
0

No, a reference can't be bound to another object after it has been initialised.

int r1 =1001;
int r2  =10023;
int &r3 = r1;
r3=r2;

Here, r3 is bound to the object r1 with value 1001. Then the name r3 behaves exactly as if you were using the name r1 instead - it's like an alias. When you assign the value of r2 to that object, r1 now contains the value 10023.

Comments

0

Here is your code with comments, which may help you understand:

int r1 = 1001;  // r1 now holds the value 1001
int r2 = 10023; // r2 now holds the value 10023
int &r3 = r1;  // using r3 is now like using r1
r3=r2;  // same as r1=r2; r1 now holds the value 10023
r3 = 999; // same as r1 = 999; r1 now holds the value 999
r3 = 555; // same as r1 = 555; r1 now holds the value 555
int r4 = 11; // r4 now holds the value 11
r3=r4; // same as r1 = r4; r1 now holds the value 11
r3 = 10177; // same as r1 = 10177; r1 now holds the value 10177
cout<<r3<<endl; // same as printing r1 which is 10177
cout<<r1<<endl; // prints 10177
cout<<r2<<endl; // prints 10023
cout<<r4<<endl; // prints 11

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.