The topic of references in C++ will be covered in this tutorial with simple examples.
When a variable is declared as a reference, it will become an alternate name for an existing variable simply by pointing toward the existing memory rather than generating new memory. These variables are also referred to as alias variables or link variables. The problem with the pointer is that we can alter the existing variable address in the future, which will make it insecure. For the purpose of solving this issue, reference variables are used.
As we all know C++ mainly supports 2 types of variables :
An ordinary variable is a kind of variable that mainly holds a value of some type. For example, suppose we constructed or created a variable of type int, which signifies that the variable will be able to hold the value of the type integer.
A variable that contains the address of another variable is known as a pointer. The value that this pointer points to can be retrieved through dereferencing.
References are a different type of variable that is supported by C++. A variable that acts as an alias for yet another variable is called the reference.
An ampersand (&) operator can be used in order to create a reference. Suppose we are creating a variable, it takes up some memory space. We have the ability in order to create a reference of the variable; as a result, we may use the variable's name or reference to retrieve the original variable.
Syntax
data_type &refernce_variabe=value_variable;
For Example
int a=7;
int &z=a;//now here you can see both z and a have the same address
The variables "a" and "z" in the preceding example are pointing toward the same memory address. As a result, 'z' has now become the alias name for 'a.' Any modification to "a" will also affect "z," and vice versa.
For example: let us write a C++ program that will make use of references on int and double.
#include <iostream>
using namespace std;
int main () {
// declare the simple variables
int i;
double d;
// declare the reference variables
int& r = i;
double& s = d;
i = 7;
cout << " The value of i : " << i << endl;
cout << "The value of i reference : " << r << endl;
d = 11.7;
cout << "The Value of d will be : " << d << endl;
cout << "The Value of d reference will be : " << s << endl;
return 0;
}
Output:
The value of i : 7 The value of i reference : 7 The Value of d will be : 11.7 The Value of d reference will be : 11.7
With a reference type variable as well as the & operator, it can be declared.
#include <iostream>
using namespace std;
int main()
{
int a=100;
int &value=a;
std::cout << value << std::endl;
return 0;
}
Output:
100
Another name for the variable that is being referenced is references as aliases.
For Example ,
int a=50; // 'a' is a variable.
int &b=a; // 'b' will reference to a.
int &c=a; // 'c' will reference to a.
Now let us write a simple C++ program to make this more clear
#include <iostream>
using namespace std;
int main()
{
int a=100; // initialization of the variable
int &b=a;
int &c=a;
std::cout << " The value of a is :" <<a<< std::endl;
std::cout << " The Value of b is :" <<b<< std::endl;
std::cout << "The Value of c is :" <<c<< std::endl;
return 0;
}
Output:
The value of a is :100 The Value of b is :100 The Value of c is :100
A variable named "a" with the value "100" is created in the code above. Our two reference variables, b and c, both refer to the same variable "a" and have been declared. As a result, we may claim that variables "b" and "c" have access to the variable "a".
At the moment of the declaration, it must be initialized
#include <iostream>
using namespace std;
int main()
{
int a=50; // initialization of the variable
int &b=a; // b will reference to a
std::cout << "Then the value of a is " <<b<< std::endl;
return 0;
}
Output:
Then the value of a is 50
In the above code, we have created a reference variable, i.e., ‘b’. At the time of declaration, 'a’ variable is assigned to 'b'. If we do not assign it at the time of declaration, then the code would look like this:
int &b;
&b=a;
Because 'a' was not assigned at the time of declaration, the above code will fail at compile time.
This signifies that the reference variable cannot be changed/ modified since it cannot be reassigned.
#include <iostream>
using namespace std;
int main()
{
int x=100; // initialization of variable
int z=7oo;
int &y=x; // y reference to x
int &y=z; // y reference to z, but it will surely throws a compile-time error.
return 0;
}
Output:
Compilation error occurs
In the code above, the reference variable "y" is used to refer to the variable "x," and "z" is then assigned to "y." A compile-time error is generated since this reassignment is not feasible with the reference variable.
It is also possible to pass references as function parameters. It functions as an alias for a parameter but does not make a copy of the argument. Due to the fact that it doesn't duplicate the argument, it improves performance.
For example
#include <iostream>
using namespace std;
int main()
{
int a=90; // initialization of variable
int b=100; // initialization of variable
swap(a, b); // function calling
std::cout << " The value of a is :" <<a<< std::endl;
std::cout << "The value of b is :" <<b<< std::endl;
return 0;
}
void swap(int &p, int &q) // function definition
{
int temp; // declaration of the variable
temp=p;
p=q;
q=temp;
}
Output:
The value of a is :100 The value of b is :90
We are switching the values of "a" and "b" in the code above. The swap() function has received the variables 'a' and 'b' as inputs. In the swap() function, "p" stands for "a" and "q" for "b." When we switch the values of "p" and "q," this likewise switches the values of "a" and "b."
References make it simple for us to access nested data.
#include <iostream>
using namespace std;
struct profile
{
int id;
};
struct Student
{
profile p;
};
int main()
{
Student e;
int &ref=e.p.id;
ref=27;
std::cout << e.p.id << std::endl;
}
Output:
27
In the preceding code, we are attempting to obtain the 'id' of the student profile struct. Typically, we use the statement e.p.id to access this member, but if we need many accesses, this would be a very difficult task. In order to prevent this, we establish a reference variable called ref, which is another term for "e.p.id."