C++ Pointers & Functions Question:

What is const reference?

Tweet Share WhatsApp

Answer:

const references allow you to specify that the data referred to won't be changed. A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it’s a reference to const. Once a reference is bound to refer to an object, it can not be bound to refer to another object. For example:

int &ri = i;
binds ri to refer to i. Then assignment such as:
ri = j;
doesn’t bind ri to j. It assigns the value in j to the object referenced by ri, ie i;

This means, if we pass arguments to a function by const references; the function can not change the value stored in those references. This allows us to use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. The compiler will throw an error if the function tries to modify the value of a const reference.

Download C++ Pointers & Functions PDF Read All 32 C++ Pointers & Functions Questions
Previous QuestionNext Question
What is const pointer?Explain what is NULL pointer and void pointer and what is their use?