C++ References

In C++, we use a reference to create an alias for a variable. We can use the reference variable to access or modify the variable.


Create a C++ Reference

We use the ampersand sign to create a reference. For example,

string& ref_city = city;

Here,

  • string - datatype of the variable
  • & - denotes we are creating a reference
  • ref_city - name of the reference variable
  • city - the variable for which reference is created

Example: C++ Reference

#include <iostream>

using namespace std;

int main() {

    string city = "Paris";

    // create a reference to the variable
    string& ref_city = city;
    
    // display the variable
    cout << "Variable Value: " << city << endl;
    cout << "Reference Value: " << ref_city << endl;

    return 0;
}

Output

Variable Value: Paris
Reference Value: Paris

In the above example, we have used the reference variable ref_city to display the value of the variable city.


Modify Variables Using References

We can modify a variable by simply assigning a new value to the reference variable. For example,

#include <iostream>

using namespace std;

int main() {

    string city = "Paris";

    // create a reference to the variable
    string& ref_city = city;
    
    // display the variable
    cout << "Variable Value = " << city << endl;
    cout << "Reference Value = " << ref_city << endl;

    // modify the variable using reference
    ref_city = "New York";

    // display the variable
    cout << endl << "After Modification: " << endl;
    cout  << "Variable Value = " << city << endl;
    cout << "Reference Value = " << ref_city << endl;

    return 0;
}

Output

Variable Value: Paris
Reference Value: Paris

After Modification:
Variable Value: = New York
Reference Value: New York

Important Points

1. Placement of the & sign

We can place the & sign with data type or with a variable while creating a reference. However, the standard practice is to use the sign along with the data type. For example,

// create a variable
string city = "Paris";

// valid but not a standard practice
string &ref_city = city;

// valid and a standard practice
sring& ref_city = city;

2. References Initialization

We must initialize references at the time of declaration.

// create a variable
string city = "Paris";

// incorrect code [reference not initialized]
string& ref_city;
ref_city = city;

// correct code
string& ref_city = city;

3. Reference With a New Variable

Once we create a reference to a variable, it cannot be changed to refer to another variable. For example,

#include <iostream>

using namespace std;

int main() {

    string city1 = "Paris";

    // create a reference to the variable
    string& ref_city = city1;
    
    // display the variable
    cout << "city1 = " << city1 << endl;
    cout << "ref_city = " << ref_city << endl;
    
    string city2 = "New York";
    
    // trying to modify the ref_city reference variable to refer to city2
    // but it assigns the value of city2 to the variable city1
    ref_city = city2;

    // display the variables
    cout << endl << "city1 = " << city1 << endl;
    cout << "city2 = " << city2 << endl;
    cout << "ref_city = " << ref_city << endl;

    return 0;
}

Output

city1 = Paris
ref_city = Paris

city1 = New York
city2 = New York
ref_city = New York

Here, when we try to modify the reference variable ref_city to refer to the variable city2 in the line my_city = city2;, the reference variable my_city is not modified to refer to city2.

Rather, the value of city2 is assigned to the variable city1, as the reference variable ref_city refers to the variable city1.


Frequently Asked Questions

What are the types of references?

There are two main types of references in C++:

  • lvalue references
  • rvalue reference

lvalue references

lvalue is a variable or object, and it may exist beyond the expression it is created. For example,

int a = 5;

Here, we have created the variable a in this expression, however, it may be used in any other expression of the program. In fact, it can be used in the scope it is declared on.

As the name suggests, lvalue references are used to refer to lvalues (variables or objects).

rvalue references

rvalue is a value (like literal, result of operator, return value of function, etc.) that doesn't exist beyond the expression that it is created on.

int a = 5;

Here, 5 is the rvalue. It cannot be accessed from another expression.

Note: All references that we have used in this article are lvalue references. rvalue references are generally used in move semantics, which is beyond the scope of this tutorial.

What are the differences between references and pointers?

1. A reference is an alias for a variable, however, a pointer is a different variable that stores the memory address of a variable. For example,

cout << "s = " << s << endl;
string s = "Paris";

string* pointer_to_s = &s;
// prints the memory address of s
cout << "pointer_to_s = " << pointer_to_s << endl;
string& ref_s = s;
// prints the string s
cout << "ref_s = " << ref_s << endl;

2. We use the dereference operator * to access the value pointed by a pointer. However, we can directly use the reference without any operator.

// print the value of the variable using pointer
cout << "*pointer_to_s = " << *pointer_to_s << endl;

// print the value of the variable using reference
cout << "ref_s = " << ref_s << endl;

3. We can change the pointer to point to another variable. For example,

// create variable city1 and city2
string city1 = "New York";
string city2 = "Las Vegas";

// create a pointer to city1
string* point_city = &city1;

// print the value pointed by point_city
cout << "*point_city = " << *point_city << endl;

// change the pointer to point to city2
point_city = &city2;
cout << "*point_city = " << *point_city << endl;

However, we cannot change the reference to refer to another variable.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges