What is the difference between const reference and reference in a parameter?

Some times use of const qualifier is useful with reference. Many newbies ask these questions, “What is the difference between const reference and reference in a parameter in C++?”.

So in this post, I will give the answer to the question of what difference between const reference and reference as a parameter in the function.

Knowledge of reference and const are primary prerequisites of this question. So it is my recommendation that if you are learning C++ you must read the below articles.

 

Consider the below-mentioned functions.

Case1:

//passing reference with const
void myFun1(const int& rValue);


Case2:

//passing reference
void myFun2(int& rValue);

 

Now my question is what is the difference between these two functions?

First, let’s understand the function after that you will be self capable to give the answer to this question.

Case 1:

void myFun1(const int& rValue);

The first function myFunc1 will prevent the modification of the passed parameter. See the below example.

#include<iostream>

using namespace std;

void myFun1(const int& rValue)
{
    rValue = 3;
}

int main()
{
    int data = 4;

    myFun1(data);

    return 0;
}

Output:

What is the difference between const reference and reference in a parameter

In the above example, rValue is a reference to const int, so if you try to modify the const you will get the compiler error.

Always remember that pass the reference with const when don’t want to change the passed parameter.

 

Case 2:

void myFun2(int& rValue);

If you want to modify the passed parameter value, you must not use the const with reference. Consider the below example code.

#include<iostream>

using namespace std;

void myFun2(int& rValue)
{
    rValue = 3;
}

int main()
{
    int data = 4;

    cout<< "Before calling myFun2 data is " << data<<endl;

    myFun2(data);

    cout<< "After calling myFun2 data is " << data<<endl;

    return 0;
}

Output:

difference between const reference and reference in function parameter

In the above example, rValue is a reference to int, so you allow to modify the value of the passed parameter.

 

Both reference to const type and reference to type (const T &arg vs T &arg) also differ in terms of function overloading. Consider the below code,

#include<iostream>

using namespace std;

void foo(int& rValue)
{
    cout << "Reference\n";
}

void foo(const int& rValue)
{
    cout << "Reference with const\n";
}

int main()
{
    int data1 = 4;
    const int data2 = 4;

    foo(data1);
    foo(data2);
    foo(4);

    return 0;
}

Output:

Reference
Reference with const
Reference with const

 

The first function foo(int& rValue) is called only with variables that are non-const.

int data1 = 4;
foo(data1);  // prints Reference

 

The second function foo(const int& rValue) is called if you pass it a variable that is const, or if you pass it a literal value.

 const int data2 = 4;

foo(data2); //prints Reference with const
foo(4); //prints Reference with const

 

Now I believe you are able to understand the difference between reference and reference to const.

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *