C++

What is const Reference in C++

In C++, references are like pointers but have distinct differences. Firstly, references cannot be assigned a null value. Secondly, arithmetic operations cannot be performed on references. Thirdly, once a reference is initialized, it cannot be made to refer to another variable. Also, references cannot be dereferenced with the operator (*).

When it comes to passing arguments to functions, there are three ways to do it:

  • Pass by value
  • Pass by reference
  • Pass by const reference

In this tutorial, we will specifically discuss const reference, its declaration, and its implementation in detail.

What is const Reference in C++?

A const reference in C++ is a reference with the const keyword. It is simply a reference to constants. When we initialize a const reference the value that const reference points cannot be changed and it will remain unchanged throughout the code.

Declaring a reference as const is useful in situations where we want to prevent changes to the object being referred to, while still allowing efficient access to the object’s value.

How to Declare const Reference in C++?

In C++, const reference can be declared in the following ways:

1: Declare const Reference as Function Parameters

When we declare a function to take a const reference as a parameter, it can read the value of the referenced object but cannot modify it. This is useful when we want to pass an object to a function without allowing the function to modify it.

The following is the code for such a case:

#include <iostream>

void printValue(const int& value) {

std::cout << "The value is: " << value << std::endl;

}

int main() {

  int val = 15;

printValue(val);

  return 0;

}

This is a function named printValue that takes a const reference to an integer as a parameter and prints the value of the integer to the console.

Output

2: Declare const Reference as Class Member Variables

When a class member variable is declared as a const reference, it can be initialized in the constructor initialization list and then used throughout the class without being modified. This can help improve the efficiency of the code by avoiding unnecessary copies and modifications of objects.

The following is the code for such a case:

#include <iostream>

class MyClass {

public:

MyClass(const int& value) : m_value(value) {}

  void printValue() const {

std::cout << "The value is: " << m_value << std::endl;

  }

private:

  const int& m_value;

};

int main() {

  int val = 15;

MyClass myObject(val);

myObject.printValue();

  return 0;

}

The above code defines a class that has a constructor which takes a const reference to an integer as a parameter and initializes a private member variable m_value with it. The class also has a member function printValue that prints the value of m_value to the console. In the main function, an object of type MyClass is created with an integer value 15, and its printValue function is called to print the value to the console.

Output

3: Declare const Reference as Local Variables

A const reference can also be used as a local variable to reference an existing object without making a copy.

The following is the code for such a case:

#include <iostream>

int main() {

  int val = 15;

  const int& ref = val;

std::cout << "Value: " << val << std::endl;

std::cout << "Reference: " << ref << std::endl;

  return 0;

}

In the above code, we declare an integer variable value and initialize it with the value 15. We then declare a constant reference to an integer ref and initialize it with the value of value.

Output

Conclusion

Th const reference is a useful feature in C++, that acts like a pointer, but with some important differences. It cannot modify the value of the variable it’s connected to and can be used in various ways, such as function parameters, class member variables, and local variables. To better understand the use cases of const references in C++, this article provides detailed guidelines with coding examples for each of these ways.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.