Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I assign a new value to a reference, and how can I make a reference refer to something else?

Tags:

c++

reference

I have couple of questions related to usage of references in C++.

  1. In the code shown below, how does it work and not give a error at line q = "world";?

    #include <iostream>  using namespace std;  int main() {   char *p = "Hello";   char* &q = p;   cout <<p <<' '<<q <<"\n";   q = "World"; //Why is there no error on this line   cout <<p <<' '<<q <<"\n"; } 
    1. How can a reference q be reinitialized to something else?

    2. Isn't the string literal, p = "Hello", a constant or in read-only space? So if we do,

      q = "World"; 

      wouldn't the string at p which is supposed to be constant be changed?

  2. I have read about C++ reference type variables as they cannot be reinitialized or reassigned, since they are stored 'internally' as constant pointers. So a compiler would give a error.

    But how actually a reference variable can be reassigned?

    int i;  int &j = i;  int k;  j = k; //This should be fine, but how we reassign to something else to make compiler flag an error? 

    I am trying to get hold of this reference, and in that maybe missed some key things related, so these questions.

So any pointers to clear this up, would be useful.

like image 983
goldenmean Avatar asked Aug 24 '11 19:08

goldenmean


People also ask

Can you change the value of a reference?

This is not possible, and that's by design. References cannot be rebound. You are not changing the reference, you are assigning the value of k to the object referred to by x .

Can we assign a value to a reference variable?

However, in Python, variable is just a name given to an object in the memory. Even if we assign its value to another variable, both are in fact referring to same object in memory. This can be verified by id() function. It therefore is clear that in Python, we can not create reference to a variable.

How do you assign a reference to a variable?

To assign reference to a variable, use the ref keyword. A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters. Declare the reference parameters using the ref keyword.

Can we create a reference to a reference?

An array of references is acceptable. We can also create a reference to a reference.


1 Answers

    • a) It cannot, the line you quote doesn't change the reference q, it changes p.
    • b) No the literal is constant, but p is a pointer which points at a literal. The pointer can be changed, what is being pointed to cannot. q = "world"; makes the pointer p point to something else.
  1. You seem to think that this code

    int i; int &j = i; int k; j = k; 

    is reassigning a reference, but it isn't. It's assigning the value of k to i, j still refers to i. I would guess that this is your major misunderstanding.

like image 64
john Avatar answered Sep 30 '22 20:09

john