An Introduction to References in C++

C++ provides the pointer and reference to refer the object indirectly. But it is our choice to use the pointer or reference in our code for accessing the object indirectly. But C++ insist to use the reference but it is up to you and your requirement.

Basically, reference defines an alternative name for an object or you can say that it is an alias of a referring object. In programming, we define the reference of an object by using the & with followed by the reference name.

For example,

//create an variable
int data = 6;

//rOffData refer to data
int& rOffData = data;

 

When we initialize a variable, the value of the initializer (could be an lvalue or rvalue) is copied into the variable. But when we initialize a reference, we bind the initializer with the reference. It means if we change the value of the reference, the value of the variable could change automatic vice versa. Let see an example code to understand this concept.

In this example, I am creating an integer variable and assigning 6 to it. In the second steps, I am creating an integer reference and initialize it by data. Now you can see when I am changing the value of the reference, the value of the data is also changing.

#include <iostream>

using namespace std;

int main()
{
    //create an variable
    int data = 6;

    //rOffData refer to data
    int& rOffData = data;

    //print data and rOffData
    cout <<"rOffData = "  << rOffData << endl ;
    cout <<"data = "  << data << endl ;

    // Assign 27 to the rOffData
    rOffData = 27;

    //print data and rOffData
    cout << "After change value of rOffData" << endl;

    cout <<"rOffData = "  << rOffData << endl ;
    cout <<"data = "  << data << endl ;

    return 0;
}

Output:

references in c++

 

You should remember, once a reference bind with any object, it will not bind with another object. Also, reference must be initialized at the time of creation.

#include <iostream>

using namespace std;

int main()
{
    int& rOffData;

    cout <<rOffData;

    return 0;
}

Output: error: ‘rOffData’ declared as reference but not initialized

 

#include <iostream>

using namespace std;

int main()
{
    //create an variable
    int data1 = 6;
    int data2 = 27;

    //rOffData refer to data1
    int& rOffData = data1;

    cout << "Value of data1 and rOffData" << endl;
    cout << "rOffData = " << rOffData << endl;
    cout << "data1 = " << data1 << endl;

    //Only copy the value of data2 (reference does not change)
    rOffData = data2;

    cout << "\n\nValue of data1 and rOffData after assigning to data2 to rOffData" << endl;
    cout << "rOffData = " << rOffData << endl;
    cout << "data1 = " << data1 << endl;

    //Assign value to reference
    rOffData = 24;

    cout << "\n\nValue of data1 and rOffData after assigning to 24 to rOffData" << endl;
    cout << "rOffData = " << rOffData << endl;
    cout << "data1 = " << data1 << endl;

    return 0;
}

Output:

references in c++

 

You can see in the above that when assigning data2 to the rOffData, the binding is not changing only the value of data2 is copying to the rOffData.

Note: References in C++ are not an object but it is just another name of an already existing object.

 

Some important points related to the references in C++

 

1. A reference must be initialized at the time of creation. See the below few examples,

int fun1(int) noexcept;

void fun()
{
    int data = 10; //integer variable
    int& rData = data; // rData refers to data
    
    int& rData1 = rData; // rData1 refers to what rData refers to, that is, to data
    
    int (&rfun)(int) = fun1; // rfun refers to the function rfun1
    rfun (data); // calls function fun1
    
    int aData[3]; //array of 3 integer
    int (&rAdata)[3] = aData; // rAdata refers to the array aData
    rAdata[0] = data; // modifies aData[0]
}

 

2. References in C++ can not refer to the NULL.

 

3. Once reference bind with the object, it will not bind with another object.

 

4. According to the C++ draft, “An initializer can be omitted for a reference only in a parameter declaration, in the declaration of a function return type, in the declaration of a class member within its class definition, and where the extern specifier is explicitly used”.

int& rData1; // error: initializer missing

extern int& rData2; // it is ok

 

5. The type of reference must match the type of the object to which it refers.

#include <iostream>

using namespace std;

int main()
{
    double dval = 3.14;
    int &ri = dval;

    return 0;
}

Output:

error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’

 

6. A reference to const may refer to an object that is not const, a literal or more general expression.

#include <iostream>

using namespace std;

int main()
{
    int data = 42;
    // rData1 bound to data
    int &rData1 = data;

    // rData2 also bound to data it will but cannot be used to change data
    const int &rData2 = data;

    // rData3 also bound to data it will but cannot be used to change data
    const double &rData3 = data;
    // rData4 refers to temporary with value 6
    const int &rData4 = 6;

    return 0;
}

You can also see this Article,

7.  Lvalue reference declares using the &.

8. An rvalue reference declares using the &&.