const Keyword in C++
Constant is something that doesn't change. In C language and C++ we use the keyword
const to make program elements constant.
const keyword can be used in many contexts in a C++ program. It can be used with:
- Function arguments and return types
- Class Data members
- Class Member functions
1) Constant Variables in C++
If you make any variable as constant, using
const keyword, you cannot change its value. Also, the constant variables must be initialized while they are declared.
const int i = 10;
const int j = i + 10; // works fine
i++; // this leads to Compile time error
In the above code we have made
i as constant, hence if we try to change its value, we will get compile time error. Though we can use it for substitution for other variables.
2) Pointers with
const keyword in C++
Pointers can be declared using const keyword too. When we use
const with pointers, we can do it in two ways, either we can apply
const to what the pointer is pointing to, or we can make the pointer itself a constant.
Pointer to a
This means that the pointer is pointing to a
const int* u;
u is a pointer that can point to a
const int type variable. We can also write it like,
char const* v;
still it has the same meaning. In this case also,
v is a pointer to an
char which is of
Pointers to a
const variable is very useful, as this can be used to make any string or array immutable(i.e they cannot be changed).
To make a pointer constant, we have to put the
const keyword to the right of the
int x = 1;
int* const w = &x;
w is a pointer, which is
const, that points to an
int. Now we can't change the pointer, which means it will always point to the variable
x but can change the value that it points to, by changing the value of
The constant pointer to a variable is useful where you want a storage that can be changed in value but not moved in memory. Because the pointer will always point to the same memory location, because it is defined with
const keyword, but the value at that memory location can be changed.
NOTE: We can also have a
const pointer pointing to a
const int* const x;
const Function Arguments and Return types
We can make the return type or arguments of a function as
const. Then we cannot change any of them.
void f(const int i)
i++; // error
const int g()
Some Important points to Remember
- For built in datatypes, returning a
const or non-const value, doesn't make any difference.
const int h()
const int j = h();
int k = h();
k will be assigned the value
1. No error will occur.
- For user defined datatypes, returning
const, will prevent its modification.
- Temporary objects created while program execution are always of
- If a function has a non-const parameter, it cannot be passed a
const argument while making a call.
If we pass a
// function logic
const int* argument to the function
t, it will give error.
- But, a function which has a
const type parameter, can be passed a
const type argument as well as a non-const argument.
This function can have a
void g(const int*)
// function logic
int* as well as
const int* type argument.
4) Defining Class Data members as
These are data variables in class which are defined using
const keyword. They are not initialized during declaration. Their initialization is done in the constructor.
const int i;
cout << "\ni value set: " << i;
In this program,
i is a constant data member, in every object its independent copy will be present, hence it is initialized with each object using the constructor. And once initialized, its value cannot be changed. The above way of initializing a class member is known as Initializer List in C++.
5) Defining Class Object as
When an object is declared or created using the
const keyword, its data members can never be changed, during the object's lifetime.
const class_name object;
For example, if in the class
Test defined above, we want to define a constant object, we can do it like:
const Test r(30);
6) Defining Class's Member function as
const member functions never modifies data members in an object.
return_type function_name() const;
Example for const Object and const Member function
StarWars(int x) // constructor
i = x;
int falcon() const // constant function
can do anything but will not
modify any data members
cout << "Falcon has left the Base";
StarWars objOne(10); // non const object
const StarWars objTwo(20); // const object
objOne.falcon(); // No error
objTwo.falcon(); // No error
cout << objOne.i << objTwo.i;
objOne.gamma(); // No error
objTwo.gamma(); // Compile time error
Falcon has left the Base
Falcon has left the Base
Here, we can see, that
const member function never changes data members of class, and it can be used with both
const and non-const objecta. But a
const object cannot be used with a member function which tries to change its data members.
mutable keyword is used with member variables of class, which we want to change even if the object is of
const type. Hence,
mutable data members of a
const objects can be modified.
mutable int j;
i = 0;
j = 0;
void fool() const
i++; // will give error
j++; // works, because j is mutable
const Zee obj;
Want to learn coding and don't know where to start?
Try out our Interactive Courses for Free 🥳 😯 🤩