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++
#include <iostream>
using namespace std;
int main()
{
const int constant = 10;
cout << "Value of constant: " << constant << endl;
return 0;
}
|
Output
Value 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++
#include <iostream>
using namespace std;
int main()
{
int maxi = 10;
const int * ptr = &maxi;
cout << "Value of maxi: " << maxi << endl;
cout << "Value of maxi via pointer: " << *ptr << endl;
return 0;
}
|
Output
Value 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++
#include <iostream>
using namespace std;
void display( const int & num)
{
cout << "The number is: " << num << endl;
}
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++
#include <iostream>
using namespace std;
class MyClass {
public :
MyClass( int value)
: data(value)
{
}
void display() const
{
cout << "Data of value: " << data << endl;
}
private :
int data;
};
int main()
{
MyClass obj(42);
obj.display();
return 0;
}
|
Share your thoughts in the comments
Please Login to comment...