Interviewer And Interviewee Guide

C++ Pointers & Functions Interview Question:

What is const reference?

Submitted by: Administrator
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.
Submitted by: Administrator

Read Online C++ Pointers & Functions Job Interview Questions And Answers
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.