In C++, a reference variable is a variable that is used to refer to an existing variable. This type of variable is created using the &
operator.
We use the &
operator to create a reference variable.
For example:
string name = "Theo"; // this is a variable
string &myname = name; // this is the reference to the variable `name`
In the code written below, we will create a variable and a reference variable to refer to the initial variable.
#include <iostream>#include <string>using namespace std;int main() {string name = "Theo"; // this is a variablestring &my_name = name; // this is the reference to the variable `name`;my_name = "Leo";cout << name << "\n";cout << my_name << "\n";return 0;}
name
.my_name
. This variable is used to refer to the name
variable.my_name
to Leo
.name
.my_name
.#include<iostream>using namespace std;void swap (int& number_one, int& number_two){int temp = number_one;number_one = number_two;number_two = temp;}int main(){int first_number = 1, second_number = 2;swap(first_number, second_number);cout << first_number << " " << second_number;return 0;}
In the code written above, we swapped the values of the variables first_number
and second_number
by using the reference variable.
for
loop.#include <bits/stdc++.h>using namespace std;int main(){// creating a vector objectvector<int> my_vector{ 10, 20, 30, 40 };// modifying the elements of the vector// using a reference variablefor (int &element : my_vector){element += 5;}for (int element : my_vector){cout << element << " ";}return 0;}
In the code written above, we modified all the elements of the variable my_vector
using the reference variable.
In C++, a memory address is the location on the computer where a variable is stored. A memory address is assigned to a variable when it is created. Also, whenever a value is assigned to the variable, it is stored in the memory address.
We use the &
operator to get the memory address of a variable:
Note: It is worth noting that the memory address of a variable is represented in hexadecimal form. Also, the result in the code example given below may not be the same as the one in our program.
In the code given below, we will create a variable and use the &
operator to obtain the memory address of the variable, that is, the location at which the variable is stored on the computer.
#include <iostream>#include <string>using namespace std;int main(){// creating a stringstring name = "Theo";// obtaining the memory adresscout << &name;return 0;}
name
.&
operator right after the variable name.Now, we will determine the memory address of a reference variable using a code from before.
#include <iostream>#include <string>using namespace std;int main(){string name = "Theo"; // this is a variablestring &my_name = name; // this is the reference to the variable `name`;// obtaining the memory adress of the variablecout << "Memory adress of the variable name: " << &name << endl;// obtaining the memory adress of the reference variablecout << "Memory adress of the reference variable: " << &my_name << endl;return 0;}