How to Use Const Keyword in C++?
Last Updated :
02 Feb, 2024
In C++, the const keyword is used to declare constants or unchangeable values. It indicates an immutable object that cannot be modified. The const keyword can be applied to variables, pointers, member functions, objects, and references.
In this article, we will learn how to use the const keyword in C++.
Use of const in C++
The application of the const keyword in C++ varies depending on the context in which it is used. Let's look at each use and see how it varies for different situations:
1. Declare a Variable as a Constant
We can declare a constant variable using the const keyword. The value of constant variables cannot be changed after initialization
Syntax
const type name = initial_value;
Example
C++
// C++ program to demonstrate the use of const keyword
#include <iostream>
using namespace std;
int main()
{
const int constant = 10;
cout << "Value of constant: " << constant << endl;
// Uncommenting the below line would produce a
// compilation error constant = 20;
return 0;
}
OutputValue of constant: 10
2. Declare a Pointer to a Constant
Declaring pointer as a constant is similar to the declaring constant variable. In this, we can modify the address that the pointer is storing but we cannot modify the value it its pointing to.
Syntax
int maxi = 5;
const int* ptr = &maxi;
In the above syntax, We first initialize a "maxi" variable. After that, we declare a ptr pointer to a constant integer. After that, The value of maxi cannot be modified through ptr. It can only point to the address of maxi.
Example
C++
// C++ Program Demonstrating Pointer to a Constant Integer
#include <iostream>
using namespace std;
int main()
{
int maxi = 10;
// Pointer to a constant integer
const int* ptr = &maxi;
cout << "Value of maxi: " << maxi << endl;
cout << "Value of maxi via pointer: " << *ptr << endl;
// Error: Attempting to modify the value via the pointer
// *ptr = 10;
return 0;
}
OutputValue of maxi: 10
Value of maxi via pointer: 10
3. Constant Arguments
Constant arguments in functions are used to prevent modification of the argument value within the function. This is useful when we pass arguments by reference and don’t want the function to change the argument’s value.
Syntax
void functionName(const dataType& argumentName) {
// Code here
}
In the syntax above, const is used before the data type of the argument. This makes the argument a constant reference, meaning the function cannot change its value.
Example
C++
// C++ program to demonstrate constant arguments
#include <iostream>
using namespace std;
// Function with constant argument
void display(const int& num)
{
cout << "The number is: " << num << endl;
// Uncommenting the below line would produce a
// compilation error
// num = 10;
}
int main()
{
int number = 5;
display(number);
return 0;
}
In this example, the display
function takes a constant integer reference as an argument. The function can access the argument’s value but cannot modify it.
4. Constant Member Functions
The const keyword in member functions indicates that the function does not modify any member variables of the class. It promises not to modify the object's state, allowing the function to be called on constant objects of the class.
Syntax
class MyClass {
public:
void display() const {
// Code here
}
};
Example
C++
// C++ Program Demonstrating a constant member function
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int value)
: data(value)
{
}
// Display function to print the value of the data
// member
void display() const
{
cout << "Data of value: " << data << endl;
}
private:
int data;
};
int main()
{
MyClass obj(42);
// Display the value using the display method
obj.display();
return 0;
}
Similar Reads
How to use const with Pointers in C++?
In C++, the const keyword is used as a type qualifier for defining read-only (immutable) objects that cannot be modified anywhere in their lifetime. It can be used in several ways with pointers, each serving a different purpose. In this article, we will learn how to use const qualifier with pointers
3 min read
How to Use typename Keyword in C++?
In C++, the "typename" is a keyword that was introduced to declare a type. It is used for specifying that the identifier that follows is a type rather than a static member variable. In this article, we will learn how to use the typename keyword in C++. C++ typename KeywordThe typename keyword is mai
2 min read
How to Use const_iterator with a Map in C++?
In C++, a const_iterator is a type of iterator that points to constant content. This means that using a const_iterator, you can read from but not write to the element it points to. In this article, we will learn how to use a const_iterator with a map in C++ STL. Example: Input: myMap = {{âappleâ, 1}
2 min read
How to Use the Volatile Keyword in C++?
In C++, the volatile keyword is used to tell the compiler that the value of the variable declared using volatile may change at any time. In this article, we will learn how to use the volatile keyword in C++. Volatile Keyword in C++We can use the volatile keyword for different purposes like declaring
2 min read
How to Traverse Vector using const_iterator in C++?
In C++, a const_iterator is a type of iterator that points to constant content means by using a const_iterator, we can read from but cannot write to the element it points to. In this article, we will learn how to use a const_iterator with a vector in C++ STL. Example: Input: myVector = {1, 2, 3} Out
2 min read
How to Traverse a Set with const_iterator in C++?
In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It contains a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a set with const_iterator in
2 min read
How to Traverse a List with const_iterator in C++?
In C++, a list is a container used to store data in non-contiguous memory locations. It also provides a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a list with const_iterator in C++. Example Input: myList = {10,20,30,40,50}
2 min read
How to Create a Thread in C++?
A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++. How to Create a Thread in C++?In C++, the std::thread is a class template that is used t
2 min read
How to Traverse Vector Using const_reverse_iterator in C++?
In C++, a vector can be traversed in reverse order using a const_reverse_iterator. A const_reverse_iterator is a type of iterator that points to the last element in the container and moves in the reverse direction. In this article, we will learn how to traverse a vector using a const_reverse_iterato
2 min read
C++ Program to Show Use of This Keyword in Class
Here, we will see how to use this keyword in a class using a C++ program. this keyword in C++ is an implicit pointer that points to the object of the class of which the member function is called. Every object has its own this pointer. Every object can reference itself by this pointer. There are 4 wa
4 min read