Creating References in C++
Assume a variable name as a label attached to its location in memory. It is same as you have a registered name in college and nickname at home. Therefore, you can access the value using its original name or its reference name.
Example
int i = 17;
Creating a reference variable for I as follows.
int& r = i;
Read the & in these declarations as reference. Thus, “r” is another name reference initialized to “i” and read the second declaration as “s is a double reference initialised to “d”.
Example 1
include<iostream> using namespace std;
int main () {
// declare simple variables
int i;
double d;
// declare reference variables
int& r = i;
double& s = d;
i = 5;
cout << “Value of i : ” << i << endl;
cout << “Value of i reference : ” << r << endl;
d = 11.7;
cout << “Value of d : ” << d << endl;
cout << “Value of d reference : ” << s << endl;
return 0;
}
When the above code is compiled together and executed, it produces the following result −
Value of i: 5
Value of i reference: 5
Value of d: 11.7
Value of d reference: 11.7
Example 2
int s = 10;
// Declaring lvalue reference
int& l_ref = s;
// Declaring rvalue reference
int&& r_ref = 20;
–> Example for lvalue and rvalue reference
// C++ program to illustrate the
// lvalue and rvalue
include<iostream>
using namespace std;
int main()
{
// Declaring the variable
int a{ 10 };
// Declaring reference to
// already created variable
int& b = a;
// On comparing the variable and address
// the result will come same
// as both are pointing to same location
cout << (&a == &b) << endl;
return 0;
}
Explanation
The code outputs ‘true’ as b is an lvalue reference to a.
Both of them are pointing to the same memory locations. b is an alternative name to the memory assigned to a.
You can also read about the memory hierarchy.
Important Properties of Rvalue References
- Rvalue references extend the life of the object to which they are referred(assigned).
- Non-const rvalue allows for changing the value.
Note: lvalue references can be assigned to Rvalue references but not vice versa.
Example to Illustrate Lvalue and Rvalue
include<iostream>
using namespace std;
int main()
{
int a = 10;
// Declaring lvalue reference
// (i.e variable a)
int& lref = a;
// Declaring rvalue reference
int&& rref = 20;
// Print the values
cout << “lref = ” << lref << endl;
cout << “rref = ” << rref << endl;
//changing the value of lref
lref = 30;
// Changing the value of rref
rref = 40;
cout << “lref = ” << lref << endl;
cout << “rref = ” << rref << endl;
// Error – The lvalue references cant be assigned to rvalue references
// int &&ref = a;
return 0;
}
Output
lref = 10
rref = 20
lref = 30
rref = 40
Uses of Lvalue References
- Lvalues can be used as an alias to an existing object.
- They can also be used in implementing pass by reference semantics.
Example
include<iostream>
using namespace std;
// references of the parameter passed to the function swap
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
int main()
{
// initial values
int a{ 10 }, b{ 20 };
cout << “a = ” << a << ” b = ” << b << endl; // Function call –>Call by Reference
swap(a, b);
// Print the swapped values
cout << “a = ” << a << ” b = ” << b << endl;
return 0;
}
Output
a = 10 b = 20
a = 20 b = 10
Uses of Rvalue References
- They are used in working with the move constructor and move assignment.
- Cannot bind non-const lvalue reference of type ‘int&‘ to an rvalue of type ‘int’.
- Cannot bind rvalue references of type ‘int&&‘ to an lvalue of type ‘int’.
Example 1
include<iostream>
using namespace std;
// lvalue reference to the lvalue
// passed as the parameter
void printReferenceValue(int& x)
{
cout << x << endl;
}
int main()
{
// initial value
int a{ 10 };
// Function call is made lvalue & can
// be assigned to lvalue reference
printReferenceValue(a);
return 0;
}
Output
10
Example 2
include<iostream>
using namespace std;
// Declaring rvalue reference to the
// rvalue passed as the parameter
void printReferenceValue(int&& x)
{
cout << x << endl;
}
// Driver Code
int main()
{
// Given value a
int a{ 10 };
// Works fine as the function is
// called with rvalue
printReferenceValue(100);
return 0;
}
Output
100
Also check out this article - Pair in C++
Frequently Asked Questions
What is Rvalue in C++?
In C++, an Rvalue is a temporary object or a value that is not associated with an object. An Rvalue can be thought of as a value that is used to initialize an object, or as a value that is returned from a function, but is not stored in a variable.
What is Lvalue in C++?
Lvalue refers to an object that persists in memory and has a name. An Lvalue can be thought of as an object that can be assigned a value, whereas an rvalue is a temporary object that cannot be assigned a value.
What do you mean by references in C++?
In C++, a reference is a way to refer to an existing object, using another name. A reference is similar to a pointer, but with some important differences. When you create a reference, you are creating an alias for an existing object, rather than creating a new object.
Conclusion
In this article, we have discussed Lvalue and Rvalue references in C++. We have discussed how we can create the references in C++. We have also discussed its implementation.
Also, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, and many more! If you wish to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!
But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Recommended Readings: